【问题标题】:ListView with ContextMenu option带有 ContextMenu 选项的 ListView
【发布时间】:2012-10-18 02:10:04
【问题描述】:

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

我用过registerForContextMenu(listViewTotes)onCreateContextMenuonContextItemSelected

谢谢!

【问题讨论】:

  • 你用的是什么版本的安卓?

标签: android listview textview contextmenu


【解决方案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...
  • 实际上,我从 blogspot.com 下载了您所说的 ListView 代码。然后我添加上面的代码,它在我的 Android 手机上运行良好。现在,我在那里添加了更多代码,你可以看看它们。如果代码没有帮助,我很抱歉。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多