Kishore
Kishore

Reputation: 2051

Android expandable list: groups with different layouts

I want to display different child layout for each group like:

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

            if (convertView == null) {

                switch (groupPosition){

                case 0:
                    LayoutInflater inflater =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.child_row, null);
                    TextView tvPlayerName = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                case 1:
                    LayoutInflater inflater1 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater1.inflate(R.layout.child_row1, null);
                    TextView tvPlayerName1 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName1.setText(arrChildelements[groupPosition][childPosition]);
                    break; 

                case 2:
                    LayoutInflater inflater2 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater2.inflate(R.layout.child_row2, null);
                    TextView tvPlayerName2 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName2.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                case 3:             
                    LayoutInflater inflater3 =  (LayoutInflater) myContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    convertView = inflater3.inflate(R.layout.child_row3, null);
                    TextView tvPlayerName3 = (TextView) convertView.findViewById(R.id.tvPlayerName);
                    tvPlayerName3.setText(arrChildelements[groupPosition][childPosition]);
                    break;

                }


            } return convertView;
    }

My problem is while clicking on the groups, the child list is inter changing between groups.

Can anybody tell me that what i am doing wrong?

Upvotes: 2

Views: 3325

Answers (2)

Jefri Singh
Jefri Singh

Reputation: 31

if (convertView == null)

remove that from the custom adapter at child view

it working fine for all

Upvotes: 3

Ali Alnoaimi
Ali Alnoaimi

Reputation: 2317

it is because of

if (convertView == null)

remove it and it should work fine, if it worked as it should after removing this if statement, return it and add if statements inside if (convertView == null) to make it appear as you want it, since removing if (convertView == null) will make scrolling much slower and uses a lot more ram.

for example you can put all the views in one xml files or in one view, and then inside the switch method just use setVisibility to show the part of the layouts that suits the current list item.

Upvotes: 7

Related Questions