0

我有一个带有 CursorTreeAdapter 和 CheckedTextView 的 ExpandableListActivity 显示。它在单击时工作正常(我必须手动切换 CheckedTextView,但那是因为这是一个 ExpandableListView)但是当适配器在光标上调用 requery 时,检查的项目不是正确的(与以前一样)。

您对问题所在有任何线索吗?

4

3 回答 3

0

查看我的答案以保持检查项目的状态。. 使用 Checkbox访问Android ExpandableListView,控制选中状态

于 2011-05-31T05:09:15.560 回答
0

这很可能是 Android 的列表回收在起作用。这是一个具有类似问题的 SO 问题:Android: Handle ListView Recycle。在您的情况下,您的适配器处理 TextView 标签的更新,因此这些看起来是正确的,但它不关心复选框状态。我有类似的问题,并通过提供我自己的适配器来解决它,基于SimpleExpandableListAdapter,但使用覆盖的getChildView方法,该方法同时处理 textview 内容和复选框状态。

在这种情况下,我发现查看相应的 Android 源文件以找出真正发生的事情很有启发性。在这种情况下,有趣的文件是ExpandableListView.javaSimpleExpandableListAdapter.java(我基于适配器的文件,在你的情况下是CursorTreeAdapter.java

于 2011-02-15T20:13:48.923 回答
0

我有同样的问题ExpandedListView。展开/折叠其他组组件后,Switch或其他Checkable控件未保存语句。我通过将状态保存到 childData 列表/数组中解决了这个问题。

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;
}
}

我想它会帮助你

于 2017-02-10T11:31:19.313 回答