删除项目从列表视图中使用浮动上下文菜单 [英] Deleting item from listview using floating context menu
问题描述
已经有使用onItemLongClick方法来删除一个列表视图项目的能力,但我宁愿使用一个浮动上下文菜单来做到这一点。
Already had the ability to delete a listview item using an onItemLongClick method but I'd rather use a floating context menu to do this.
下面是code我目前有浮动上下文菜单。我跟着这让我将它设置,然后试图寻找一个类似的例子,我在做什么,但没有找到任何合适的文档。
Below is the code I currently have for the floating context menu. I followed the documentation which helped me set it up and then tried to search for a similar example to what I'm doing but couldn't find anything appropriate.
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenu.ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.payments_context, menu);
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.edit:
return true;
case R.id.delete:
return true;
default:
return super.onContextItemSelected(item);
}
}
这是在code,我不得不删除我的列表视图中的项目之前,我决定改用浮动上下文菜单
This is the code I had to delete the items in my listview before I decided to switch to a floating context menu
public boolean onItemLongClick (AdapterView<?> parent, View view, int position, long id)
{
String temp = paymentTitle.get(position).toString();
paymentTitle.remove(position);
paymentDate.remove(position);
reminderDate.remove(position);
reminderTime.remove(position);
paymentVal.remove(position);
mDatabase = new MOSDatabase(this);
SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
readableDB.delete("PaymentTable", "PTITLE=?",
new String[]{temp});
aa.notifyDataSetChanged();
return false;
}
如果有人能告诉我如何得到这个浮动上下文菜单中的工作,我会非常感激。我没有做过,只是还没有编辑的方法,这是我必须做我得到这个完成之后。
If someone could advise me on how to get this floating context menu working I'd be really grateful. I don't have the edit method done just yet, it's what I have to do after I get this completed.
推荐答案
如果我理解正确的话,您可以通过以下code点击位置获得该项目的在ListView索引:
If I understand correctly, you can get the index of the item in the ListView at the click position by using the following code:
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
使用位置
,你可以重复使用 onItemLongClick
pretty的的code多的是:
Using position
, you can reuse the code of onItemLongClick
pretty much as is:
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
int position = info.position;
switch (item.getItemId()) {
case R.id.edit:
return true;
case R.id.delete: {
String temp = paymentTitle.get(position).toString();
paymentTitle.remove(position);
paymentDate.remove(position);
reminderDate.remove(position);
reminderTime.remove(position);
paymentVal.remove(position);
mDatabase = new MOSDatabase(this);
SQLiteDatabase readableDB = mDatabase.getWritableDatabase();
readableDB.delete("PaymentTable", "PTITLE=?",
new String[]{temp});
aa.notifyDataSetChanged();
}
return true;
default:
return super.onContextItemSelected(item);
}
}
您可能想看看答案这个的问题。
You might want to look at the answer to this question.
这篇关于删除项目从列表视图中使用浮动上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!