从选定的ListView项上下文菜单中的设置标题 [英] Set title of context menu from the selected Listview item

查看:112
本文介绍了从选定的ListView项上下文菜单中的设置标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何设置上下文菜单的标题从选定列表视图项目?下面这是我的主要活动

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

我需要设置 menu.setHeaderTitle R.id.name 。我知道另一个similer 问题,但它没有提到关于处理复杂的的ListView 多textviews。

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.

推荐答案

使用从 ContextMenuInfo 参数 onCreateContextMenu()方法:

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

这篇关于从选定的ListView项上下文菜单中的设置标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆