在自定义的ListView获取复选框正常工作
我做什么都对我的列表视图自定义行布局:在自定义的ListView获取复选框正常工作
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="@+id/chkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
我使用这个布局我的适配器的活动:
adapter = new AdapterCustomBoxes(context, R.layout.custom_check_row, (ArrayList<Map<String, String>>) list_values);
list.setAdapter(adapter);
list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
而对于一个适配器,这里是我的适配器的getView方法:
public class AdapterCustomBoxes extends ArrayAdapter<Map<String, String>> {
private List<Map<String, String>> list;
private List<Map<String, String>> orig_list;
private Context upper_context;
private View view;
public AdapterCustomBoxes(Context context, int textViewResourceId, ArrayList<Map<String, String>> items) {
super(context, textViewResourceId, items);
this.list = items;
this.orig_list = items;
this.upper_context = context;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
view = convertView;
if (view == null) {
LayoutInflater vi = (LayoutInflater)upper_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.custom_check_row, null);
}
Map<String, String> selectedgroup = new HashMap<String, String>();
selectedgroup = (Map<String, String>) list.get(position);
String itemtext = (String) selectedgroup.get("item_text");
TextView row_text = (TextView) view.findViewById(R.id.text);
row_text.setText(itemtext);
return view;
}
}
现在来这里的地步IG等困惑。当我检查其中一个复选框时,点击一个被检查。但没有任何逻辑,其他行的某些复选框也会被检查。如果我然后滚动列表,每个框的检查状态可能会改变,从一次我碰到这一行到下一次。
那么这里有什么问题?
我已经尝试过做一个onclicklistener添加到适配器的视图,然后设置明确的复选框cheched /未选中时,该监听器被触发,但是这也正如我所料不工作,我点击了一个框,另一个被检查。
所以我想这是一个回收的意见问题?我是否需要单独存储选中的状态并每次在getView方法中恢复它是否可以成为解决方案?还是有更简单的方法?请帮忙;)
那么有人可以给我一个提示吗? Thnaks很多!
---编辑---
所以我现在想保存的复选框的状态是创造一个地图:
private Map<View, Boolean> itm = new HashMap<View, Boolean>();
和保存状态时,单击在一个盒子在getView方法中:
CheckBox chkbox = (CheckBox) view.findViewById(R.id.chkbox);
chkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
itm.put(buttonView, isChecked);
}
});
if(itm.containsKey(chkbox)){
iteminfo_row_chkbox.setChecked(itm.get(chkbox));
}else{
iteminfo_row_chkbox.setChecked(false);
}
对不起,如果这是完全错误的方法,但不应该这样工作?结果是一样的整个列表和检查状态不正确,我做错了什么?
一些题外话评论:
首先,你不需要这样的:
Map<String, String> selectedgroup = new HashMap<String, String>();
你可以只是这样做:
Map<String, String> selectedgroup = (Map<String, String>) list.get(position);
此外,你确定你需要TextView?该复选框也可以保存文本。
关于你的问题,你如何保存哪个复选框被选中?您应该在返回视图时设置复选框,特别是在重新使用视图时。
- 编辑 -
你为什么不建立这样一个私有的类?
private class ListItem{
String text;
boolean value;
}
然后代替List<Map<String,String>>
你可以只是有一个List<ListItem>
。
关于onCheckedChangeListener
,您可以使用复选框中的文本迭代列表以找到正确的位置,然后更改该值。如果文本不是唯一的,则可以使用 而不是使用onCheckedChangeListener
,您可以在ListView中使用一个OnItemClickListener
,它包含点击的位置。
好吧我想我知道了,我使用了selectedgroup map中的一个静态id值作为itm映射中的键值来查找复选框的选中/未选中状态。使用视图是错误的方法,因为它们不能用作关键。
谢谢你的题外话!目前我不保存选择状态,似乎是问题所在?那么,我该如何设法保存状态,以及如何将这些信息返回给活动?非常感谢。 – homtg
看到我上面的编辑。 –