ACluelessProgramer
ACluelessProgramer

Reputation: 308

Having trouble having more than one TextView in a ExpandableListView

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?

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

MainActivity:

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

ExpandablelistAdapter:

    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;


    }

}

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

    }

Upvotes: 1

Views: 370

Answers (1)

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

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).

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.


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

    ...
}

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

Upvotes: 2

Related Questions