在 ExpandableListView 中拥有多个 TextView 时遇到问题

Having trouble having more than one TextView in a ExpandableListView

我似乎无法在我的可扩展列表中初始化另一个文本视图。我已经有了一个,但我正在尝试在同一个 child 中添加第二个。我为 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;


    }

}

组Class:

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 次(从 019)。

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


问题在于你的 Group class。请注意 setName()setDetail() 如何将数据仅保存到 Name class 成员。因此,您会看到数据的两个副本。

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 class 成员并更新相应的 getDetail()setDetail() 方法即可解决您的问题。