Question

I am able to implement checkedtextview in expandablelistview. But when I check certain child item , some child item of other parent gets selected as well . I am unable to figure out why am I facing such problem? Any one faced similar situation ? Thanks for help in advance.

EDIT :

As suggested I created boolean multidimensional array in customadapter: `

Boolean[][] categoryx = new Boolean` [5][5];

There is getchildView method in custom adapter :

 @Override
      public View getChildView(int groupPosition, final int childPosition,
          boolean isLastChild, View convertView, ViewGroup parent) {
          System.out.print(groupPosition);
          System.out.println(childPosition);
        final String children = (String) getChild(groupPosition, childPosition);
        CheckedTextView text = null;
        if (convertView == null) {
          convertView = inflater.inflate(R.layout.listrow_details, null);
        }
        text = (CheckedTextView) convertView.findViewById(R.id.detail_textView1);
        text.setText(children);

        convertView.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {

              ((CheckedTextView )v).toggle();
          }
        });
        return convertView;
      }

But I am confused in which method should I toggle my values using accessed childposition.

EDIT 2 : My custom adapter where I am trying to save state :

import android.app.Activity;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CheckedTextView;
import android.widget.TextView;
import android.widget.Toast;

public class MyCategoryAdapter extends BaseExpandableListAdapter {

      private SparseArray<Groupx> groups;
      public LayoutInflater inflater;
      public Activity activity;
      Boolean[][] categoryx = new Boolean [5][5];

      public MyCategoryAdapter(Activity act, SparseArray<Groupx> groups) {
        activity = act;
        this.groups = groups;
        inflater = act.getLayoutInflater();
      }

      @Override
      public Object getChild(int groupPosition, int childPosition) {
        return groups.get(groupPosition).children.get(childPosition);
      }

      @Override
      public long getChildId(int groupPosition, int childPosition) {
        return 0;
      }

      @Override
      public View getChildView(int groupPosition, final int childPosition,
          boolean isLastChild, View convertView, ViewGroup parent) {
          System.out.print(groupPosition);
          System.out.println(childPosition);
        final String children = (String) getChild(groupPosition, childPosition);
        CheckedTextView text = null;
        if (convertView == null) {
          convertView = inflater.inflate(R.layout.listrow_details, null);
        }
        text = (CheckedTextView) convertView.findViewById(R.id.detail_textView1);
        text.setText(children);

        convertView.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              //Integer lol = (Integer) v.getTag();
              ((CheckedTextView )v).toggle();
          }
        });
        return convertView;
      }

      @Override
      public int getChildrenCount(int groupPosition) {
        return groups.get(groupPosition).children.size();
      }

      @Override
      public Object getGroup(int groupPosition) {
        return groups.get(groupPosition);
      }

      @Override
      public int getGroupCount() {
        return groups.size();
      }

      @Override
      public void onGroupCollapsed(int groupPosition) {
        super.onGroupCollapsed(groupPosition);
      }

      @Override
      public void onGroupExpanded(int groupPosition) {
        super.onGroupExpanded(groupPosition);
      }

      @Override
      public long getGroupId(int groupPosition) {
        return 0;
      }

      @Override
      public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
          convertView = inflater.inflate(R.layout.listrow_group, null);
        }
        Groupx group = (Groupx) getGroup(groupPosition);
        ((CheckedTextView) convertView).setText(group.stringx.toString());
        ((CheckedTextView) convertView).setChecked(isExpanded);
        return convertView;
      }

      @Override
      public boolean hasStableIds() {
        return false;
      }

      @Override
      public boolean isChildSelectable(int groupPosition, int childPosition) {
        return false;
      }
    } 
Was it helpful?

Solution

This is a problem that regularly arises due to the misunderstanding of the ListView row recycling.

The thing is, you are only keeping the checked attribute in your view. Now, ListViews reuse cells that disappear from the screen for saving memory, and so, once a row is checked, another one will appear checked as well, once the original one has been recycled.

To circumvent this, you will have to save the checked state in your ListView's data source, and set the appropriate checked value in your ListView adapter.

OTHER TIPS

Set singlechoicemode:true in xml.

Use below Override methods from BaseExpandableListAdapter

public int getChildType(int groupPosition, int childPosition) {
 //define a condition here to differentiate between your child views.
}

public int getChildTypeCount() {
 return 2; <-- this is the number of views for your child
}

Hope this will help.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top