【发布时间】:2013-09-10 15:37:50
【问题描述】:
我有一个列表和可以显示它的 ArrayAdapter(基本上作为 CheckBox 的列表)。 MyObejct 的字段很少,其中一个是启用布尔值的。
我怎么做,点击 CheckBox 会改变相应的模型?比如:
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;
}
}
【问题讨论】:
-
您能在此处发布更多代码,即您的适配器吗?
标签: android checkbox onclick android-arrayadapter