Amit
Amit

Reputation: 703

Cannot Obtain ListView position clicked in Context Menu

I'm trying to create a context menu, and do something to the line that was clicked in the ListView, but when ever I try to use info.position

Here is my code :

listView = (ListView) findViewById(R.id.listView1); // The list view
listView.setOnItemLongClickListener(new OnItemLongClickListener()
    {
        public boolean onItemLongClick(AdapterView<?> arg0, View arg1,int pos, long id)  // One Long Click
        {
            registerForContextMenu(listView); // Registering the context menu
            Log.e(TAG, "Long Click!");
            return true;
        }
    });

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
                                ContextMenu.ContextMenuInfo menuInfo) 
{      
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optmenu, menu);
}

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

    switch(item.getItemId())
    {
        case R.id.item3:
            RemoveTask(info.position);
        break;
    }

    return true;
}

in onContextItemSelected(MenuItem item), whenever I try to use the line info.position(suppost to return an int of the listview's position), the program just crashes...

(It doesnt get inside RemoveTask, I have chcked... even if I try to print info.position using Log, it crashes my application)...

Thanks!

Edit: I'm using the same code as I posted here, yet suddenly, the menu isnt even showing up anymore...

Upvotes: 0

Views: 175

Answers (2)

Mike M.
Mike M.

Reputation: 39191

If you want to register a View for a Context Menu, you do not need to set an OnLongClickListener for the View. In your case, the ListView can be registered for the Menu in the onCreate() method of your Activity:

@Override
public void onCreate (Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    ...
    ...
    listView = (ListView) findViewById(R.id.listView1);
    registerForContextMenu(listView);
}

Get rid of the code for the OnLongClickListener and the rest of your code should remain the same:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo)
{
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.optmenu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item)
{
    AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    switch(item.getItemId())
    {
        case R.id.item3:
            RemoveTask(info.position);
            break;
    }

    return true;
}

Upvotes: 1

MikeWallaceDev
MikeWallaceDev

Reputation: 1468

Try calling

registerForContextMenu(listView); // Registering the context menu

from your onCreate method right underneath

listView = (ListView) findViewById(R.id.listView1); // The list view

Upvotes: 1

Related Questions