private List<String> list = new ArrayList<String>();
//为listview创建成员对象
private ListView lv;
private ArrayAdapter<String> myadapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_manager);
list.add("first");
list.add("second");
list.add("third");
lv=(ListView)findViewById(R.id.lv);
//设置Adapter
myadapter=new ArrayAdapter<TrueFalse>(ManagerListActivity.this, android.R.layout.simple_list_item_1,lstQuestions);
lv.setAdapter(myadapter);
//设置ContextMenu,长按listitem时触发
lv.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu arg0, View arg1,
ContextMenuInfo arg2) {
// TODO Auto-generated method stub
arg0.setHeaderTitle("选择操作");
arg0.add(0, 0, 0, "删除");
arg0.add(0, 1, 0, "未定");
}
});
}
//设置菜单内容和事件
@Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
//获取点击的item的id
String id = String.valueOf(info.id);
switch(item.getItemId()){
case 0:
Toast.makeText(ManagerListActivity.this, id, Toast.LENGTH_SHORT).show();
/*移除list的某项数据,注意remove()里的数据只能是int,这里用了强制转换,将long转换成int*/
list.remove((int)info.id);
//更新listview的数据
myadapter.notifyDataSetChanged();
return true;
case 1:
Toast.makeText(ManagerListActivity.this, "11", Toast.LENGTH_SHORT).show();
return true;
}
return super.onContextItemSelected(item);
}
}
持续完善