Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ExpandableListView with multiple layouts

I apologize for my English but translated with google. I need help for my application, I'm using a ExpandableListView with 3 different layouts but I'm having problems that I have not found an answer on various forums. Basically if the method getChildView () check if the convertView is null, I offsets all layouts or even me repeat them several times. If I do not make that check, you see the layout as it should, but the values in the EditText erased if I click on another group or if you skim the list until no more to show the layout with the EditText. I hope I was clear enough and I hope that answers are equally clear, unfortunately I have been all over the internet but could not find the answer I was looking for. Thanks in advance to those who will help me

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

            View view = convertView;
            ViewHolder holder;

            if (view == null) {
                    holder = new ViewHolder();

                    if (groupPosition == 0) {
                            convertView = infalInflater.inflate(R.layout.adv_item, null);
                            holder.btn1 = (ImageButton) convertView
                                            .findViewById(R.id.button1);
                            holder.btn2 = (ImageButton) convertView
                                            .findViewById(R.id.button2);
                            holder.btn3 = (ImageButton) convertView
                                            .findViewById(R.id.button3);
                            holder.et1 = (EditText) convertView.findViewById(R.id.editText1);
                            holder.et2 = (EditText) convertView.findViewById(R.id.editText2);
                            holder.et3 = (EditText) convertView.findViewById(R.id.editText3);
                            holder.et4 = (EditText) convertView.findViewById(R.id.editText4);
                            holder.et5 = (EditText) convertView.findViewById(R.id.editText5);
                            holder.et6 = (EditText) convertView.findViewById(R.id.editText6);
                            holder.check_sc1 = (CheckBox) convertView
                                            .findViewById(R.id.item_check_sc1);
                            holder.check_sc2 = (CheckBox) convertView
                                            .findViewById(R.id.item_check_sc2);
                            holder.check_sc3 = (CheckBox) convertView
                                            .findViewById(R.id.item_check_sc3);
                            holder.check_oo = (CheckBox) convertView
                                            .findViewById(R.id.item_check_oo);
                            convertView.setTag(holder);
                    }
                    if (groupPosition == 1) {
                            convertView = infalInflater.inflate(R.layout.visual_item, null);
                    }
                    if (groupPosition == 2) {
                            convertView = infalInflater.inflate(R.layout.routes_item, null);
                    }
            } else {
                    holder = (ViewHolder) convertView.getTag();
            }
            return convertView;
    }
like image 374
itc.opra Avatar asked Dec 12 '22 01:12

itc.opra


1 Answers

Right now your list doesn't "know" that it has three different types of records. You need to "tell" it that by overriding the next function of the Adapter:

@Override
public int getViewTypeCount() {
    return 3;     // <-- Here goes your view types count
}

Also you need to override the Adapter's function to figure out which type of view to use:

@Override
public int getItemViewType(int position) {
    return getItemViewType(position);  // <-- Here must be your logic to get the type of the item
}

So now Android has a pool of three types of list items you can use. And now you dont need 'if (groupPosition == 2)' (and etc.) because a ListView will use those functions to figure the right layout to pass into adapter's 'getView()' function. The above is true for simple ListAdapters.

As for ExpandableAdapters they have similar functions:

public int getGroupTypeCount ()
public int getGroupType (int groupPosition)
public int getChildType (int groupPosition, int childPosition)
public int getChildTypeCount ()

You need to override all of them, or some of them, depending whether you use different layouts for groups or not. As for your case, i suggest to try something like:

@Override
public int getChildTypeCount () {
    return 3;   <-- you have three types of views
}

@Override
public int getChildType (int groupPosition, int childPosition) {
    /*
     * In case of group 1 this function returns 1,
     * in case of group 2 it returns 2, and so on.
     */
    return groupPosition;
}
like image 156
mykolaj Avatar answered Jan 28 '23 10:01

mykolaj