【问题标题】:Set title of context menu from the selected Listview item从选定的 Listview 项中设置上下文菜单的标题
【发布时间】:2015-03-14 15:34:01
【问题描述】:

如何从选定的Listview 项目中设置上下文菜单的标题?以下是我的主要活动

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。我知道另一个类似的question,但它没有提到处理具有多个文本视图的复杂ListView

【问题讨论】:

    标签: android


    【解决方案1】:

    使用onCreateContextMenu() 方法中的ContextMenuInfo 参数:

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

    【讨论】:

    • 成功了,非常感谢。顺便说一句,我不得不更改为menu.setHeaderTitle(cursor.getString(1))
    • @BinoyBabu 你是对的方法getString() 需要int 而不是String。我累了,我在想方法cursor.getString(cursor.getColumnIndex("name"));
    • 别担心。不管怎样,你是我的英雄。睡一觉好吗?
    • @Luksprog 这太棒了;)非常感谢!
    【解决方案2】:

    我知道这是一篇很老的帖子,也是正确的答案。但是,在今天使用它时,我遇到了一些我想添加的东西。

    ContextMenuInfo 参数用于查找启动 ContextMenu 的确切项目位置,即我们的 adpater 项目。

    因此,它可以使用该位置info.position 返回适配器的getItem() 方法中定义的类型的项目,如上面的getItem() 方法返回一个Cursor 对象。

    (在我的例子中,它返回了一个模型类,然后我意识到要通过menu.setHeaderTitle() 设置标题,我可以传递我的模型支持的方法,例如model.getItamName()

    另外,请记住,如果您的 AdapterView 包含任何标题,您必须在使用 menuInfo 获取位置时排除它们。喜欢,

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

    希望这对某人有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多