在View的#CreateContextMenu中创建上下文菜单时,AdapterView.AdapterContextMenuInfo为null

时间:2015-03-06 07:33:32

标签: java android android-listview

我有一个应用程序,它对活动中的片段有很多看法。其中许多视图都需要上下文菜单,因此我必须在视图中onCreateContextMenu而不是活动onCreateContextMenu中定义上下文菜单。当我尝试访问

中的AdapterContextMenuInfo对象时出现问题
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

在activity {的onContextItemSelected方法中..对象(info)返回null。在这种情况下如何获得此对象?谢谢。 在View类中,我有: 在它的构造函数中,我从它的视图初始化listview并注册它 上下文菜单:

class ContactsList  extends RelativeLayout implements OnItemClickListener,OnItemLongClickListener
    ListView pageList;
    ContactsListAdapter adapter = new ContactsListAdapter();

public ContactsList(final MainActivity activity, RelativeLayout mainXmlLayout ) {   
super(activity);
setLayoutParams( new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
        RelativeLayout.LayoutParams.MATCH_PARENT ));

    addView(mainXmlLayout);

    pageList = (ListView)findViewById(R.id.contactslist);
    pageList.setAdapter(adapter);

    Utils.loadContactsList();
pageList.setOnItemClickListener(this);

activity.registerForContextMenu(pageList);
}//end constructor

    @Override
    public void onCreateContextMenu(ContextMenu menu) {
        super.onCreateContextMenu(menu);

       MenuInflater inflater = MainActivity.getActivity().getMenuInflater();


    inflater.inflate(R.menu.contacts_context_menu, menu);
    menu.setHeaderTitle("Contact"); 

    }
@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view,
            int position, long id) {
//....
        return false;
    }
    public void onItemClick(AdapterView<?> adView, View target, int position, long id) {
//.....
}

and in the  activity I have: 

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

         Utils.logErrorMessage("info = "+info);
        switch (item.getItemId()) {...}
    }//end method



}

1 个答案:

答案 0 :(得分:1)

我刚刚通过改变应用程序的结构来解决了这个问题。我没有在视图中定义上下文菜单,而是在ListViews上设置了需要上下文菜单的标签,并使用switch语句在父活动的onCreateContextMenu中定义它们。然后我在activity的onContextItemSelected方法中以正常方式访问它们。 e.g。

首先,我在父视图的类构造函数的列表中设置标记:

e.g。

pageList.setTag("Contacts");

然后在包含视图的活动中:

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
    MenuInflater inflater = getActivity().getMenuInflater();

switch (v.getTag().toString()) {
    case "Contacts":
        inflater.inflate(R.menu.contacts_context_menu, menu);
        menu.setHeaderTitle("Contact");
        break;
    case "Chats":
        inflater.inflate(R.menu.chat_contacts_context_menu, menu);
        menu.setHeaderTitle("Contact");
        break;
    case "GroupChats":
        inflater.inflate(R.menu.groups_context_menu, menu);
        menu.setHeaderTitle("Your Group");  
        break;

    default:
        break;
    }      
    }

    @Override
        public boolean onContextItemSelected(MenuItem item) {
        super.onContextItemSelected(item);

        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

         Utils.logErrorMessage("info = "+info);
        switch (item.getItemId()) {
        case R.id.chatwithcontact:
    //...do something
     return true;
    }

    }//end method