在ListView中获取所选项目

问题描述 投票:0回答:2

我有一个ListView,我在长按下显示一个菜单。在onContextItemSelected中,我有一个处理动作的逻辑。我需要知道ListView中的哪个项目被选中。

  • 如果用户选择的项目不是子菜单,我可以使用MenuItem.getMenuInfo()。
  • 但是,如果用户选择子菜单,则MenuItem.getMenuInfo()将返回null。

如果有子菜单,如何确定所选项目的正确方法是什么?

菜单:

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

onContextItemSelected:

 @Override
 public boolean onContextItemSelected(MenuItem Item) {
  AdapterView.AdapterContextMenuInfo AdapterInfo = (AdapterView.AdapterContextMenuInfo)Item.getMenuInfo(); // getMenuInfo returns null if V1 is selected (submenu) but works if selected item isn't submenu, e.g., 'Edit' as per above XML
  UserItem SelectedItem = MyAdapter.getItem(AdapterInfo.position); // MyAdapter is the instance of class that inherits from ArrayAdapter<UserItem> i.e. adapter for ListView
 }
android
2个回答
1
投票

你需要做3件事

1.在switch语句中提供一个处理菜单项的默认部分

2.将info.position保存到Activity中的成员变量

3.当您检测到该信息为空时,请使用您在步骤2中创建的var

private int mParentContextMenuListIndex;

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterView.AdapterContextMenuInfo info;
    try {
        info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    } catch (ClassCastException e) {
        Log.e(TAG, "bad menuInfo", e);
        return false;
    }
    //if info == null, it means we have a submenu to deal with, use the saved info.position
    int idxOfList = (info!=null) ? info.position : this.mParentContextMenuListIndex;
    ...
    switch (item.getItemId()) {
        case R.id.context_menu_item_1:
            ... //use idxOfList instead of info.position
            return true;
        case R.id.context_menu_item_2:
            ... //use idxOfList instead of info.position
            return true;
        case R.id.context_menu_item_3:
            ... //use idxOfList instead of info.position
            return true;
        case R.id.context_submenu_item_1:
            ... //use idxOfList instead of info.position
            return true;
        case R.id.context_submenu_item_2:
            ... //use idxOfList instead of info.position
            return true;
        default: //can handle submenus if we save off info.position
            this.mParentContextMenuListIndex = idxOfList;
    }//switch
    return super.onContextItemSelected(item);
}

0
投票

为什么不将onItemClickLitener()用于listview中的所有项目而不将它们分为munu项目和子菜单项?

 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

            String s = listView.getItemAtPosition(i).toString();
            }
    });

所以i将是你的项目位置。

如果您仍想使用子菜单项,则需要实现onOptionsItemSelected(MenuItem item)方法并根据子菜单项的ID捕获onclick事件。

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
        case 10:
            //do smth here
            return true;
        case 15:
            //do smth here
            return true;
        case 20:
            //do smth here
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
© www.soinside.com 2019 - 2024. All rights reserved.