在 ExpandableListView 中有多个 TextView 时遇到问题 [英] Having trouble having more than one TextView in a ExpandableListView

查看:26
本文介绍了在 ExpandableListView 中有多个 TextView 时遇到问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎在我的可扩展列表中初始化另一个文本视图时遇到问题.我已经有了一个,但我正在尝试在同一个孩子中添加第二个.我为 textview(ltv) 创建了一个新的 setter 和 getter,并且我不断收到 java.lang.ArrayIndexOutOfBoundsException: length=0;每当我尝试用我的另一个初始化它时,我的 logcat 中的 index=0 错误.请看我下面的代码,我错过了什么?

I seem to be having trouble initializing another textview in my expandablelist. I already have one, but I'm trying to add a second one in the same child. I made a new setter and getter for the textview(ltv),and I keep getting java.lang.ArrayIndexOutOfBoundsException: length=0; index=0 error in my logcat whenever I try to initialize it with my other one. Please see my code below, what am I missing?

大小为 5,例如我每个数组有 20 个元素.

size is 5 and I have 20 elements per array for an example.

主要活动:

public class Brandspage extends Activity {

    private ExpandListAdapter ExpAdapter;
    private ExpandableListView ExpandList;
    private ArrayList<Group> ExpListItems;
    private TextView brandtv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_brandspage);
        Button b1 = (Button) findViewById(R.id.button4);
        b1.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                Intent i = new Intent(Brandspage.this, MainActivity.class);
                startActivity(i);
            }
        });


        ExpandList = (ExpandableListView) findViewById(R.id.exp_list);
        ExpListItems = SetStandardGroups();
        ExpAdapter = new ExpandListAdapter(Brandspage.this, ExpListItems);
        ExpandList.setAdapter(ExpAdapter);


    }


    public ArrayList<Group> SetStandardGroups() {


        String group_names[] = {"A-G", "H-N", "O-U", "V-Z"
        };

        String bold[] = {"1", "2", "3", "4", "5", 
"6", "7", "8", "9", "10", 
"11", "12", "13", "14", "15","16",
"17","18","19","20"
//This is the array I'm trying to initialize with the ltv textview 





        };


      String names[] = { "Brazil", "Mexico", "Croatia", "Cameroon",
            "Netherlands", "chile", "Spain", "Australia", "Colombia",
            "Greece", "Greece", "chile", "chile", "chile", "chile","Colombia","Colombia","Colombia","Colombia","Colombia"




        };


        int Images[] = {R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher, R.drawable.ic_launcher,
            R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,R.drawable.ic_launcher,


        };


        ArrayList<Group> list = new ArrayList<Group>();
        ArrayList<Child> ch_list;
        int size = 5;
        int j = 0;
        int k = 0;


        for (String group_name : group_names) {
            Group gru = new Group();
            gru.setName(group_name);
            ch_list = new ArrayList<Child>();

                for ( j=0; j < size; j++) {
                    Child ch = new Child();
//this is not working?
                    ch.setDetail(bold[j]);
                    ch.setName(names[j]);



                    ch.setImage(Images[j]);
                    ch_list.add(ch);



                }
                gru.setItems(ch_list);
                list.add(gru);

                size = size + 5;


            }
            return list;
        }
    }

可扩展列表适配器:

    public class ExpandListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private ArrayList<Group> groups;

    public ExpandListAdapter(Context context, ArrayList<Group> groups) {
        this.context = context;
        this.groups = groups;
    }
    @Override
    public Object getChild(int groupPosition, int childPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.get(childPosition);

    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        Child child = (Child) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.child_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.country_name);
        ImageView iv = (ImageView) convertView.findViewById(R.id.flag);
        TextView ltv = (TextView) convertView.findViewById(R.id.large);
        tv.setText(child.getName());
        iv.setImageResource(child.getImage());
        //Trying to do this textview ltv
        ltv.setTypeface(null, Typeface.BOLD);
        ltv.setText(child.getDetail());




            return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        ArrayList<Child> chList = groups.get(groupPosition).getItems();
        return chList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return groups.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Group group = (Group) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inf = (LayoutInflater) context
                    .getSystemService(context.LAYOUT_INFLATER_SERVICE);
            convertView = inf.inflate(R.layout.group_item, null);
        }
        TextView tv = (TextView) convertView.findViewById(R.id.group_name);
        tv.setText(group.getName());
        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {return false;


    }

}

团体课:

import java.util.ArrayList;

public class Group {

    private String Name;
    private ArrayList<Child> Items;

    public String getName() {
        return Name;
    }

    public String getDetail() {
        return Name;
    }

    public void setDetail(String name) {
        this.Name = name;
    }

        public void setName(String name) {
            this.Name = name;
        }

        public ArrayList<Child> getItems() {
            return Items;
        }

        public void setItems(ArrayList<Child> Items) {
            this.Items = Items;
        }

    }

推荐答案

你所有的数组都有不同的大小,但你在同一个 for 循环中迭代它们.你的 bold[]5 元素,names[]10Images[]8;但是,您在 for 循环中访问它们,使用变量 j 设置为迭代 20 次(从 0> 到 19).

All of your arrays are of different sizes but you're iterating over them in the same for loop. Your bold[] has 5 elements, names[] has 10, Images[] only 8; but, you're accessing them in your for loop using the variable j set to iterate 20 times (from 0 to 19).

这就是您收到 ArrayIndexOutOfBounds 异常的原因.它们都应该具有相同的长度并匹配您的 size 变量,以便您初始化 size 数量的 Child 对象.

That's why you're getting the ArrayIndexOutOfBounds exception. They should all be of the same length and match your size variable to let you initialize size number of Child objects.

<小时>问题在于您的 Group 类.请注意 setName()setDetail() 如何仅将数据保存到 Name 类成员.因此,您会看到数据的两个副本.


The problem lies with your Group class. Notice how both setName() and setDetail() are saving the data to Name class member only. Hence, you are seeing two copies of your data.

public class Group {

    private String Name;
    private String Detail; // Add a new Detail member

    private ArrayList<Child> Items;

    public String getName() {
        return Name;
    }

    public String getDetail() {
        return Detail; // change getter
    }

    public void setDetail(String Detail) {
        this.Detail = Detail; // change setter
    }

    public void setName(String name) {
        this.Name = name;
    }

    ...
}

只需添加一个新的 Detail 类成员并更新相应的 getDetail()setDetail() 方法,就像我上面所做的那样解决您的问题.

Just add a new Detail class member and update the corresponding getDetail() and setDetail() methods as I've done above to fix your problem.

这篇关于在 ExpandableListView 中有多个 TextView 时遇到问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆