DolDurma
DolDurma

Reputation: 17299

android get listview item by contextmenu

In my application I'm register ContextMenu on Listview and I want to get clicked Listview item by context menu. For example if I have two row in list view with this structure:

public class StructReceiveSms{

    public int     userID;
    public String  username;
}

my adapter can be show username in listview. now i'm in below code can define conext menu on list view:

public class FragmentSmsReceiveMaster extends Fragment {

    private static final Boolean        DEBUG    = true;
    public ArrayAdapter                 adapter;
    private ArrayList<StructReceiveSms> receiveSmsArray;
        .
        .
        .
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        .
        .
        .
        smsView = (ListView) view.findViewById(R.id.listView);
        smsView.setAdapter(adapter);
        registerForContextMenu(smsView);
        .
        .
        .
}
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        String[] menuItems = getResources().getStringArray(R.array.SmsMasterContextMenu);
        for (int i = 0; i < menuItems.length; i++) {
            menu.add(Menu.NONE, i, i, menuItems[i]);
        }
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
        int menuItemIndex = item.getItemId();
        String listItemName = adapter.getItem(info.position) + "";

        /* GET CLICKED LISTVIEW ITEM AFTER CHOOSE CONTEXTMENU MENU ITEMS */

        Toast.makeText(G.currentActivity, listItemName, Toast.LENGTH_SHORT).show();
        return true;
    }
}

Now after click on context menu items I can get which clicked by user by menuItemIndex but I can not get which Listview's items in onContextItemSelected function. for example ater opening context menu on first item I can get userID and username and show it. How to do this, thanks

Upvotes: 2

Views: 2040

Answers (1)

Mike M.
Mike M.

Reputation: 39191

Since your Adapter's data list consists of StructReceiveSms objects, the adapter.getItem(info.position) call in onContextItemSelected() will return the list item the context menu was opened for, but it will need to be cast to the StructReceiveSms type. From this, you can get the userID and username you want.

public boolean onContextItemSelected(MenuItem item)
{
    ...
    StructReceiveSms listItem = (StructReceiveSms) adapter.getItem(info.position);
    String selectedName = listItem.username;
    int selectedId = listItem.userID;
    ... 
}

This is assuming you've not overridden the getItem() method of the Adapter to return something else, but I imagine you would've shown that if you had.

Upvotes: 1

Related Questions