我有两个问题。1)如何在带有复选框的自定义列表视图中单击复选框时打开选项菜单,以及 2)如何在选中复选框时删除列表视图项目(它必须同时适用于多个复选框)。
这是我的代码..
CheckListModel.java
public class CheckListModel
{
private String name;
private boolean selected;
public CheckListModel(String paramString)
{
this.name = paramString;
this.selected = false;
}
public String getName()
{
return this.name;
}
public boolean isSelected()
{
return this.selected;
}
public void setName(String paramString)
{
this.name = paramString;
}
public void setSelected(boolean paramBoolean)
{
this.selected = paramBoolean;
}
}
ListwithCheckboxAdapter.java
public class ListwithCheckboxAdapter extends ArrayAdapter<CheckListModel>
implements OnCheckedChangeListener {
private final List<CheckListModel> list;
private final Activity context;
ViewHolder viewHolder;
public ListwithCheckboxAdapter(Activity context, List<CheckListModel> list) {
super(context, R.layout.listcheckbox, list);
this.context = context;
this.list = list;
}
static class ViewHolder {
protected TextView text;
protected CheckBox checkbox;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.listcheckbox, null);
viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox.setOnCheckedChangeListener(this);
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
return view;
}
@Override
public void onCheckedChanged(CompoundButton cb, boolean isChecked) {
// TODO Auto-generated method stub
CheckListModel element = (CheckListModel) viewHolder.checkbox.getTag();
element.setSelected(cb.isChecked());
}
}
收藏活动.java
public class FavouriteActivity extends Activity implements OnItemClickListener {
ListView mainListView;
List<CheckListModel> list;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
getMenuInflater().inflate(R.menu.fav_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.delete:
case R.id.deselect:
break;
default:
break;
}
return true;
}
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.favourites);
// Find the ListView resource.
mainListView = (ListView) findViewById(R.id.list);
ArrayAdapter<CheckListModel> adapter = new ListwithCheckboxAdapter(
this, getModel());
mainListView.setAdapter(adapter);
}
private List<CheckListModel> getModel() {
list = new ArrayList<CheckListModel>();
list.add(get("Mitesh"));
list.add(get("Agrawal"));
return list;
}
private CheckListModel get(String s) {
// TODO Auto-generated method stub
return new CheckListModel(s);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}