带有上下文菜单选项的ListView

3
首先声明,我的英语不太好。我有一个像这样的ListView和另一个TextView。我的问题是我要在那里放一个contextMenu,但是我做不到。我花了很多时间研究这个问题,但是无法找到解决方法。我已经使用了registerForContextMenu(listViewTotes), 还有 onCreateContextMenuonContextItemSelected。谢谢!

你使用的是哪个版本的安卓系统? - Ragunath Jawahar
1个回答

1

我建议您使用OnItemLongClickListener()。看起来,OnItemClickListener()没有响应registerForContextMenu(arg0)。

如果要使用OnItemLongClickListener(),示例代码如下:

public void onCreate(Bundle icicle) {
    super.onCreate(icicle);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.my_list);
    adapter = new MyAdapter(this,getModel());
    listView.setAdapter(adapter);
    listView.setOnItemLongClickListener(new PlayListOnItemLongClickListener());
}

private class PlayListOnItemLongClickListener implements AdapterView.OnItemLongClickListener {
    public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
        registerForContextMenu(arg0);

        return false;
    }
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    getMenuInflater().inflate(R.menu.context_playlist_operation, menu);
    menu.setHeaderIcon(R.drawable.ic_launcher);
    menu.setHeaderTitle("What do you want to do");
}

在/ListViewTest/res/menu/context_playlist_operation.xml中

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <group>
        <item
            android:id="@+id/context_playlist_remove_playlist"
            android:title="@string/app_name"
            />
    </group>
</menu>

这段代码不起作用。我在适配器上有一个onlongclicklistener(),现在它可以工作了,但是我放了一个TextView... - NorbertFD
实际上,我从blogspot.com下载了你说你有的ListView代码。然后我添加了像上面那样的代码,在我的Android手机上运行得很好。现在,我在那里添加了更多的代码,你可以看一下。如果这些代码没有帮助到你,我很抱歉。 - David

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接