0

I have an ExpandableListActivity with a CursorTreeAdapter and CheckedTextView displayed. It's working fine on click (I have to toggle the CheckedTextView by hand but that's because this is an ExpandableListView) but when the adapter call requery on the cursor, items that are checked are not the right one (as equal as before).

Do you have any clue on what the problem is ?

fedj
  • 3,452
  • 1
  • 22
  • 21
  • Btw, with a CheckBox and listeners, it doesn't work either. Seems like ExpandableListView is keeping the state of checkboxes by position without keeping group position too – fedj Sep 30 '10 at 15:06

3 Answers3

0

I had the same issue with the ExpandedListView. The Switch or another Checkable controls did not save statement after expanding/collapsing other group components. I have solved this issue by saving state into childData list/array.

public class ExpandPrefAdapter extends BaseExpandableListAdapter {

private static final String TAG = ExpandPrefAdapter.class.getName();

private List<String> mGroupTitle = new ArrayList<>();
private Context mContext;
private List<List<ExpandableListItem>> mChildData = new ArrayList<>();

public ExpandPrefAdapter(Context context, final List<String> groupTitle, final
List<List<ExpandableListItem>> childData) {
    mContext = context;
    mGroupTitle = groupTitle;
    mChildData = childData;
}

@Override
public View getChildView(int listPosition, final int expandedListPosition,
                         boolean isLastChild, View convertView, ViewGroup parent) {

    LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rootView = inflater.inflate(R.layout.content_pref_entry, parent, false);

    final int groupPos = listPosition;
    final int childPos = expandedListPosition;

    ExpandableListItem item = (ExpandableListItem) getChild(listPosition, expandedListPosition);

    TextView textViewPrefTitle = (TextView) rootView.findViewById(R.id.text_view_pref_title);
    textViewPrefTitle.setText(item.getTitle());

    final Switch switchView = (Switch) rootView.findViewById(R.id.switch_pref);
    switchView.setChecked(mChildData.get(groupPos).get(childPos).isChecked());
    switchView.setOnCheckedChangeListener((v, b) -> mChildData.get(groupPos).get(childPos).setChecked(b));
    return rootView;
}

@Override
public int getChildrenCount(int listPosition) {
    return mChildData.get(listPosition).size();
}

@Override
public Object getChild(int listPosition, int expListPosition) {
    return mChildData.get(listPosition).get(expListPosition);
}

@Override
public long getChildId(int listPosition, int expandedListPosition) {
    return expandedListPosition;
}

@Override
public Object getGroup(int listPosition) {
    return mGroupTitle.get(listPosition);
}

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

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

@Override
public View getGroupView(int listPosition, boolean isExpanded,
                         View convertView, ViewGroup parent) {

    String groupTitle = (String) getGroup(listPosition);
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) mContext.
                getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.content_pref_group, null);
    }
    TextView textViewGroup = (TextView) convertView
            .findViewById(R.id.text_view_pref_group_title);
    textViewGroup.setText(groupTitle);
    Log.wtf(TAG, "getGroupView(): listPosition: " + listPosition + " isExpanded: " + isExpanded);
    return convertView;
}

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

@Override
public boolean isChildSelectable(int listPosition, int expandedListPosition) {
    return false;
}
}

I guess it will help you

0

This is likely Android's list recycling at play. Here's a SO question with similar problem: Android: Handle ListView Recycle. In your case, your adapter handles updating of TextView labels, so these look correct, but it isn't concerned about checkbox state. I had similar issue and got around it by supplying my own adapter, based on SimpleExpandableListAdapter, but with overriden getChildView method, which takes care both of textview content and checkbox state.

In situations like this I find it enlightening to peek into corresponding Android source files, to find out what's really going on. In this case, the interesting files were ExpandableListView.java and SimpleExpandableListAdapter.java (the one I based my adapter on, in your case that'd be CursorTreeAdapter.java)

Community
  • 1
  • 1
Pēteris Caune
  • 43,578
  • 6
  • 59
  • 81
0

check out my answer to keep the state of the checked items. . Visit Android ExpandableListView with Checkbox, Controlling checked state

Community
  • 1
  • 1
Jianhong
  • 899
  • 9
  • 11