Eri*_*riz 69 android listview menu long-click
我有一个连接到数据库的列表视图,显示所有条目.如果用户长按listview(数据库条目)中的项目,我想要显示一个菜单,显示编辑或删除条目的选项.我怎样才能做到这一点.
直到现在,我已经尝试使用onItemLongClick监听器和吐司,显示长按的视图的ID.
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
String res = Long.toString(id);
Toast toast = Toast.makeText(this, res, Toast.LENGTH_SHORT);
toast.show();
return true;
}
Run Code Online (Sandbox Code Playgroud)
Zha*_*har 112
首先,您需要在列表视图中注册上下文菜单.
lv = (ListView) findViewById(R.id.list_view);
registerForContextMenu(lv);
Run Code Online (Sandbox Code Playgroud)
然后,只需覆盖活动方法.
/**
* MENU
*/
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.list_view) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_list, menu);
}
}
@Override
public boolean onContextItemSelected(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()) {
case R.id.add:
// add stuff here
return true;
case R.id.edit:
// edit stuff here
return true;
case R.id.delete:
// remove stuff here
return true;
default:
return super.onContextItemSelected(item);
}
}
Run Code Online (Sandbox Code Playgroud)
以下是menu_list.xml文件的示例(您必须将文件放在res/menu文件夹中).
<?xml version="1.0" encoding="utf-8"?>
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/add"
android:icon="@android:drawable/ic_menu_add"
android:title="@string/menu_add" />
<item android:id="@+id/edit"
android:icon="@android:drawable/ic_menu_edit"
android:title="@string/menu_edit" />
<item android:id="@+id/delete"
android:icon="@android:drawable/my_icon_delete"
android:title="@string/menu_delete" />
</menu>
Run Code Online (Sandbox Code Playgroud)
希望它会有所帮助.
小智 50
而不是使用onItemLongClick你可以使用
public void onCreateContextMenu(final ContextMenu menu,
final View v, final ContextMenuInfo menuInfo) {
...
}
Run Code Online (Sandbox Code Playgroud)
您可以在其中设置编辑和删除选项或任何您需要的选项.
可以处理从上下文菜单中选择的项目的操作
public boolean onContextItemSelected(final MenuItem item)
Run Code Online (Sandbox Code Playgroud)
对于上下文菜单更多信息,请点击这里.
有关分步教程,请访问此处.
另一种方法:
//Deleted individual cart items
//on list view cell long press
cartItemList.setOnItemLongClickListener (new OnItemLongClickListener() {
@SuppressWarnings("rawtypes")
public boolean onItemLongClick(AdapterView parent, View view, final int position, long id) {
final CharSequence[] items = { "Delete" };
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Action:");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
cart = cartList.get(position);
db.removeProductFromCart(context, cart);
new AlertDialog.Builder(context)
.setTitle(getString(R.string.success))
.setMessage(getString(R.string.item_removed))
.setPositiveButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(CartDetailsActivity.this, HomeScreen.class);
startActivity(intent);
}
})
.show();
}
});
AlertDialog alert = builder.create();
alert.show();
//do your stuff here
return false;
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
94840 次 |
| 最近记录: |