我有一個列表和ArrayAdapter可以顯示它(基本上作爲CheckBox的列表)。 MyObejct只有幾個字段,其中一個是啓用布爾值的。ListView與ArrayAdapter:點擊複選框時的更新模型
我該怎麼做,點擊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;
}
}
你能在這裏發佈更多的代碼,即您的Adapte R' – 2011-04-24 12:03:50