【问题标题】:Having trouble having more than one TextView in a ExpandableListView在 ExpandableListView 中有多个 TextView 时遇到问题
【发布时间】:2015-03-16 22:29:12
【问题描述】:

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

编辑:大小为 5,例如,每个数组有 20 个元素。

主活动:

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;
        }

    }

【问题讨论】:

    标签: java android arraylist expandablelistview


    【解决方案1】:

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

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


    问题在于您的Group 课程。请注意setName()setDetail() 是如何仅将数据保存到Name 类成员的。因此,您会看到两个数据副本。
    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() 方法,就像我在上面所做的那样来解决您的问题。

    【讨论】:

    • 好的,我不再有错误了,但只显示了“names[]”,并且它也在粗体文本视图中复制自身。我现在该怎么办?
    • 请发布您的完整 ExpandablelistAdapter。
    • names[]bold[] 分别有多少个元素?您能否使用当前代码更新帖子,以反映所做的任何更改?此外,size = size + 20 增加了每个后续组的子项数量(这有利于测试目的),但是名称和粗体这两个数组是否大到足以为其他子项提供数据?
    • 好了,不用全部发了。所以这两个数组都有 100 个元素并且是不同的。 SetStandardGroups() 中是否有任何其他更改?
    • 所有发布的新代码似乎都很好。如果您没有任何例外,请同时发布您的小组课程。如果是,请发布堆栈跟踪。
    猜你喜欢
    • 1970-01-01
    • 2015-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多