在列表视图中长按事件发生后创建一个菜单 [英] creating a menu after a long click event on a list view

查看:134
本文介绍了在列表视图中长按事件发生后创建一个菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有连接到数据库列表视图,显示出所有条目。我希望有一个菜单显示,如果用户长时间点击了列表视图(数据库项)一个项目,显示选项,编辑或删除的条目。我怎么能做到这一点。

到目前为止,我已经使用onItemLongClick监听器,并在其中举杯呈现多头点击视图的ID尝试。

  @覆盖
公共布尔onItemLongClick(适配器视图<>母公司视图中查看,
        INT位置,长的id){

    字符串解析度= Long.toString(ID);
     吐司面包= Toast.makeText(这一点,资源,Toast.LENGTH_SHORT);
     toast.show();

    返回true;
}
 

解决方案

onItemLongClick 您可以使用,而不是使用

 公共无效onCreateContextMenu(最终文本菜单菜单,
                    最后的视图V,最终ContextMenuInfo menuInfo){
 ...
}
 

在这里你设置的编辑选项,并删除或任何你需要。

从上下文菜单中选择的项目的操作可以在

被处理

 公共布尔onContextItemSelected(最后一个菜单项项)
 

有关上下文菜单的详细信息,请参阅这里

有关分步教程的访问时间<一href="http://www.mikeplate.com/2010/01/21/show-a-context-menu-for-long-clicks-in-an-android-listview/">here.

I have a list view connected to a database, showing a all the entries. I want a menu to show up if the user long clicks a item in the listview(database entry), showing options to edit or delete the entry. how can i do this.

Till now, I have tried using a onItemLongClick listener and a toast in it showing the id of the view long clicked.

@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;
}

解决方案

Instead of using onItemLongClick you can use

public void onCreateContextMenu(final ContextMenu menu, 
                    final View v, final ContextMenuInfo menuInfo) {
 ...
}

where you setup the options for edit and delete or whatever you need to.

The actions for the item selected from the context menu can be processed in

public boolean onContextItemSelected(final MenuItem item)

For more information on context menu see here.

For a step by step tutorial visit here.

这篇关于在列表视图中长按事件发生后创建一个菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆