带有ContextMenu选项的ListView

时间:2012-10-18 02:10:05

标签: android listview textview contextmenu

首先,我的英语不太好。 我有ListView like this而另一个TextView。 我的问题是我想在那里放contextMenu,但我不能。我失去了很多时间,但找不到解决方案。

我使用registerForContextMenu(listViewTotes)onCreateContextMenuonContextItemSelected

谢谢!

1 个答案:

答案 0 :(得分: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>