OnCreateContextMenu和ListView项目 [英] OnCreateContextMenu and ListView items

查看:167
本文介绍了OnCreateContextMenu和ListView项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有几个项目一LisView。为了这一点,我已经连接的OnItemClickListener(作为内部类),像这样的:

I have a LisView with several items. To this I've connected an OnItemClickListener (as an inner class), like this:

lv.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    Toast.makeText(ShoppingListApp02Activity.this, "List item selected:" +  
    items.get(position).getId(), Toast.LENGTH_LONG).show();
    }
});

由于是显而易见的,选择一个entriy显示条目的对象的元素,在本实施例中所选择的项目对象的ID(未在列表ID,但对象的ID,设定产生该ArrayList项目时)。这工作得很好,并让我做任何事情,我想与所选择的项目(S)。

As is obvious, selecting an entriy displays elements of the object of that entry, in this example the selected Item object's ID (not the list ID, but the objects ID, set when creating the ArrayList items). This works nicely, and enables me to do anything I want with the selected item(s).

现在,我想也有一个长按听者她,这将打开选定的ListView项的上下文菜单。我怎么做?我已经能够附加一个onCreateContextMenu监听到ListView,但我不知道怎样才能获得ArrayList的元素与onItemClickListener?

Now I'd like to also have a "long-click" listener her, which opens a context menu for the selected ListView item. How do I do that? I've been able to attach an onCreateContextMenu listener to the ListView, but I don't see how I can get the elements of the ArrayList as with the onItemClickListener?

下面是我有:

public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {  
    menu.add(0, v.getId(), 0, "Something");
    menu.add(0, v.getId(), 0, "Something else");  
}

由于OnCreateConextMenu采用不同的参数比OnItemClickListener,怎么我访问ArrayList中的元素,如在OnItemClickListener?

Since OnCreateConextMenu takes different parameters than the OnItemClickListener, how to I access the ArrayList's elements like in the OnItemClickListener?

推荐答案

如果你决定你仍然想使用上下文菜单模式:

If you decide you still want to use the context menu paradigm:

考虑本作处理列表:

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

    // Get the list
    ListView list = (ListView)v;

    // Get the list item position    
    AdapterContextMenuInfo info = (AdapterContextMenuInfo)menuInfo;
    int position = info.position

    // Now you can do whatever.. (Example, load different menus for different items)
    list.getItem(position);
    ...
}

这篇关于OnCreateContextMenu和ListView项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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