为什么MenuItem.AdapterContextMenuInfo空当我的列表视图中有一个自定义适配器? [英] Why is MenuItem.AdapterContextMenuInfo null when my list view has a custom adapter?

查看:96
本文介绍了为什么MenuItem.AdapterContextMenuInfo空当我的列表视图中有一个自定义适配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题:
我去和使用OnLongClickListener之前,有没有更好的方式来传递被点击什么来创建此上下文菜单当你的列表视图中有一个自定义适配器的信息?

下面是一些细节:

通常情况下,我的code可以做这样的事情:

 公共布尔onContextItemSelected(菜单项项){
    AdapterContextMenuInfo信息=(AdapterContextMenuInfo)item.getMenuInfo();

,然后去和快乐。

不过,自从我介绍了一个自定义适配器,item.getMenuInfo()为null。
这是一个很大的问题,因为我的code不再知道单击了哪个项目。 (我的自定义适配器使每个列表行一个复选框和文本视图)

我试过,但失败:
创建了自己的特殊AdapterContextMenuInfo(称为HasAViewMenuInfo),但是当我通过它在这个方法中,它最终被空菜单

 公共无效onCreateContextMenu(文本菜单菜单视图V,
        ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(菜单,V,新HasAViewMenuInfo(V));


解决方案

我怀疑你的问题是:


  

我的自定义适配器使每排列表
  一个复选框和一个文本视图


暂时摆脱复选框。如果你的右键菜单现在工作,那么该复选框是问题。考虑切换到 CheckedTextView 代替 - 这是更好用的ListView支持反正(例如,使用 CHOICE_MODE_MULTIPLE setItemChecked())。

我已经使用上下文菜单自定义适配器,并没有任何问题。但是,我没有使用复选框在我行。

My question: Before I go and use an OnLongClickListener, is there a better way to pass the "what was clicked to create this context menu" information when your list view has a custom adapter?

Here are some details:

Normally, my code can just do something like this:

public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

and then go on and be happy.

However, ever since I introduced a custom adapter, item.getMenuInfo() is null. This is a big problem, because my code no longer knows which item was clicked. (My custom Adapter makes each list row a checkbox and a text view)

I tried this but failed: Created my own special AdapterContextMenuInfo (called "HasAViewMenuInfo"), but when I pass it in this method, it ends up being null in the menu

public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) 
{
    super.onCreateContextMenu(menu, v, new HasAViewMenuInfo(v));

解决方案

I suspect that your problem is:

My custom Adapter makes each list row a checkbox and a text view

Temporarily get rid of the checkbox. If your context menu now works, then the checkbox is the issue. Consider switching to a CheckedTextView instead -- that's much better supported with ListView anyway (e.g., use CHOICE_MODE_MULTIPLE, setItemChecked()).

I have used context menus with custom adapters and have not had any issues. But, I have not used checkboxes in my rows.

这篇关于为什么MenuItem.AdapterContextMenuInfo空当我的列表视图中有一个自定义适配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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