user7232539
user7232539

Reputation:

Getting selected item in ListView

I have a ListView where I show a menu on the long click. In onContextItemSelected I have a logic to handle the action. I need to know what item in ListView is selected.

What is the correct way how to determine the selected item if there's a submenu ?

The menu:

<?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
 }

Upvotes: 0

Views: 115

Answers (2)

Munir
Munir

Reputation: 2558

you need to do 3 things

1.provide a default section in your switch statement that handles your menu items

2.save off the info.position to a member variable in your Activity

3.when you detect that info is null, use the var you created in step 2

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);
}

Upvotes: 1

Rainmaker
Rainmaker

Reputation: 11090

Why don't you use onItemClickLitener() for all the items in you listview without dividing them on munu items and submenu items?

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

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

So i will be your item position.

If you still want to work with submenu items you need to implement onOptionsItemSelected(MenuItem item) method and catch onclick events based on the id of submenu items .

@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);
        }
    }

Upvotes: 0

Related Questions