Binoy Babu
Binoy Babu

Reputation: 17119

Set title of context menu from the selected Listview item

How can I set title of the context menu from the selected Listview item? This below is my main activity.

public class OListActivity extends ListActivity {
......
......
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        registerForContextMenu(getListView());
        ......
......
        MatrixCursor cursor;
        cursor = NameManager.getnameList();
        startManagingCursor(cursor);
        String[] from = { "name", "info", "status", BaseColumns._ID };
        int[] to = { R.id.name, R.id.info, R.id.status };
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
                R.layout.row, cursor, from, to);
        setListAdapter(adapter);
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        menu.setHeaderTitle("Menu");// TODO Change to name of selected listview item.
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }
.....
.....

I need to set menu.setHeaderTitle to R.id.name. I'm aware of another similer question but it don't mention about dealing with a complex ListView with multiple textviews.

Upvotes: 5

Views: 6714

Answers (2)

user
user

Reputation: 87064

Use the ContextMenuInfo parameter from the onCreateContextMenu() method:

@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        AdapterView.AdapterContextMenuInfo info;
        try {
            // Casts the incoming data object into the type for AdapterView objects.
            info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        } catch (ClassCastException e) {
            // If the menu object can't be cast, logs an error.
            Log.e(TAG, "bad menuInfo", e);
            return;
        }
        Cursor cursor = (Cursor) getListAdapter().getItem(info.position);
        if (cursor == null) {
            // For some reason the requested item isn't available, do nothing
            return;
        }

        // if your column name is "name"
        menu.setHeaderTitle(cursor.getString(cursor.getColumnIndex("name")));
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.context_menu, menu);
    }

Upvotes: 14

Atul O Holic
Atul O Holic

Reputation: 6792

I know this is quite an old post and is the correct answer as well. However, while using this today I came across something I will like to add.

The ContextMenuInfo parameter is used to find the exact item position which initiated the ContextMenu i.e our adpater item.

Hence, it can return an item of type defined in the Adapter's getItem() method using that position info.position, as in the above the getItem() method returns a Cursor object.

(In my case it returned a Model class and then I realized that to set the Title via menu.setHeaderTitle() I can pass my methods that my model supports like model.getItamName())

Also, remember if your AdapterView contains any header you will have to exclude them while fetching the position using menuInfo. Like,

Cursor cursor = (Cursor) getListAdapter().getItem(info.position - yourList.getHeaderViewsCount());

Hope this helps someone. :)

Upvotes: 0

Related Questions