2010-09-30 7 views
0

J'ai une ExpandableListActivity avec un CursorTreeAdapter et un CheckedTextView affichés. Cela fonctionne bien sur clic (je dois basculer le CheckedTextView à la main mais c'est parce que c'est ExpandableListView) mais quand l'appel de l'adaptateur requery sur le curseur, les éléments vérifiés ne sont pas les bons (égaux comme avant).ExpandableListView et CheckedTextView comportement étrange

Avez-vous la moindre idée de ce qu'est le problème?

+0

BTW, avec un CheckBox et auditeurs, cela ne fonctionne pas non plus. On dirait que ExpandableListView garde l'état des cases à cocher par position sans garder la position du groupe trop – fedj

Répondre

0

Ceci est probablement la liste d'Android recyclage en jeu. Voici une question SO avec un problème similaire: Android: Handle ListView Recycle. Dans votre cas, votre adaptateur gère la mise à jour des libellés TextView, donc ceux-ci semblent corrects, mais l'état de la case à cocher n'est pas concerné. J'ai eu le même problème et l'ai contourné en fournissant mon propre adaptateur, basé sur SimpleExpandableListAdapter, mais avec la méthode getChildView surchargée, qui prend soin à la fois du contenu du texte et de l'état de la case à cocher.

Dans des situations comme celle-ci, je trouve intéressant de jeter un coup d'œil aux fichiers source Android correspondants, pour savoir ce qui se passe réellement. Dans ce cas, les fichiers intéressants ont été ExpandableListView.java et SimpleExpandableListAdapter.java (celui que je basé mon adaptateur sur, dans votre cas ce serait CursorTreeAdapter.java)

0

J'ai eu le même problème avec le ExpandedListView. Le Switch ou un autre contrôle Checkable n'a pas enregistré d'instruction après l'expansion/réduction d'autres composants de groupe. J'ai résolu ce problème en enregistrant l'état dans 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; 
} 
} 

Je suppose que cela vous aidera