我有一个带有CursorTreeAdapter和CheckedTextView的ExpandableListActivity。 它在click上工作正常(我必须手动切换CheckedTextView,但这是因为这是一个ExpandableListView)但是当适配器调用游标上的requery时,检查的项目不正确(与之前相同)。
你对这个问题有什么线索吗?
答案 0 :(得分:0)
这很可能是Android的回收列表。这是一个类似问题的问题:Android: Handle ListView Recycle。在您的情况下,您的适配器处理TextView标签的更新,因此这些看起来正确,但它不关心复选框状态。我有类似的问题,并通过提供我自己的适配器,基于SimpleExpandableListAdapter
,但使用覆盖getChildView
方法,它负责textview内容和复选框状态。
在这样的情况下,我发现窥探相应的Android源文件很有启发性,以找出真正发生的事情。在这种情况下,有趣的文件是ExpandableListView.java
和SimpleExpandableListAdapter.java
(基于我的适配器,在你的情况下是CursorTreeAdapter.java
)
答案 1 :(得分:0)
查看我的答案以保持已检查项目的状态。 。 访问Android ExpandableListView with Checkbox, Controlling checked state
答案 2 :(得分: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;
}
}
我想这会对你有所帮助