ListView 与 ArrayAdapter:单击复选框时更新模型
我有一个列表和可以显示它的ArrayAdapter(基本上作为复选框列表)。 MyObejct 有几个字段,其中之一是启用布尔值的。
我如何才能使单击复选框会更改相应的模型?像这样的东西:
public class MyObjectAdapter extends ArrayAdapter<MyObject>{
private List<MyObject> items;
public MyObjectAdapter(Context context, int textViewResourceId, List<MyObject> objects) {
super(context, textViewResourceId, objects);
this.items = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.myobject_checkbox, null);
}
MyObject myobject = items.get(position);
if (myobject!=null){
//checkbox
CheckBox cbEnabled = (CheckBox) v.findViewById(R.id.enabled);
if(cbEnabled != null){
cbEnabled.setChecked(myobject.isEnabled());
cbEnabled.setText(myobject.getName());
cbEnabled.setTag(position);
cbEnabled.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//how change model. E.g. myobject.setEnabled(((CheckBox) v).isChecked())
}
});
}
}
return v;
}
}
I have a List, and ArrayAdapter that could display it (basically as list of CheckBox). MyObejct have few fields, one of them is boolean enabled.
How I could make, that clicking on CheckBox will change corresponding model? Something like:
public class MyObjectAdapter extends ArrayAdapter<MyObject>{
private List<MyObject> items;
public MyObjectAdapter(Context context, int textViewResourceId, List<MyObject> objects) {
super(context, textViewResourceId, objects);
this.items = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.myobject_checkbox, null);
}
MyObject myobject = items.get(position);
if (myobject!=null){
//checkbox
CheckBox cbEnabled = (CheckBox) v.findViewById(R.id.enabled);
if(cbEnabled != null){
cbEnabled.setChecked(myobject.isEnabled());
cbEnabled.setText(myobject.getName());
cbEnabled.setTag(position);
cbEnabled.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//how change model. E.g. myobject.setEnabled(((CheckBox) v).isChecked())
}
});
}
}
return v;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我处理这个问题的方法是创建一个初始化列表视图的方法,一旦发生任何更改,它就会重新初始化。
每次更改列表视图中的条目时执行如下所示的调用应该使其显示为您想要的
但是我知道与 SO 社区可能使用的其他方法相比,这可能在计算上昂贵
The way I would handle this is by making a method that initialises the listview, and once anything is changed, it is re-initialised.
Doing a call like below every time an entry in the listview is changed should make it appear as you want
However I understand that this could be computationally expensive compared to other methods the SO community may use
您需要在适配器上调用
notifyDataSetChanged()
方法。这将告诉 ListView 底层数据模型已更改并且需要更新。不需要创建新的Adapter,只需更新其中的项目,然后调用我提到的方法即可。You need to call the
notifyDataSetChanged()
method on the Adapter. This will tell the ListView that the underlying data model has changed and it needs to update. There's no need to create a new Adapter, just update the items in it and then call the method I mentioned.这是工作示例。我已经在我的一个项目中实现了
Activity_main.xml
checkboxrow.xml
MainActivity.java
祝你好运:)
Here is the working example. I have implemented in my one project
activity_main.xml
checkboxrow.xml
MainActivity.java
Good Luck :)