//列表长按事件
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
HashMap item = (HashMap)parent.getItemAtPosition(position);
section =String.valueOf(item.get("name").toString());//get每-行的数据的名字
selocate = String.valueOf(item.get("locate").toString());
select_id = String.valueOf(item.get("id").toString());
//Toast.makeText(InputActivity.this, ""+section, Toast.LENGTH_SHORT).show();
selectedPosition = position;
return false;
}
});
-
响应长按监听。
-
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
//info.id得到listview中选择的条目绑定的id
String id = String.valueOf(info.id);
switch (item.getItemId()) {
case 0:
//删除事件
DBHelper dbHelper = new DBHelper();
dbHelper.open(this);
dbHelper.deleteByName(section);
dbHelper.close();
list_data.remove(selectedPosition);
adapter.notifyDataSetChanged();
Toast.makeText(mContext, id + "已删除", Toast.LENGTH_SHORT).show();
return true;
case 1:
Toast.makeText(mContext, id + "其他操作", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onContextItemSelected(item);
}
}