本文介绍了在android中使用带有列表视图的上下文菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 android 应用程序.我将有一个列表视图,并且我已经设置了一个上下文菜单以在长按列表视图项目时显示.如何从选择的列表视图项目中获取项目(比如来自列表视图的文本)textview)在选择上下文菜单中的操作之后我可以处理它吗?这是一些代码:

I am developing an android application.I will have a listview and i have set a context menu to appear when a listview item is long-pressed.How do i get the item from the listview item selected(say text from a listview textview) after an action from the contextmenu is chosen so i can process it?Here is some code:

protected void onCreate(Bundle savedInstanceState) {
    -------
    lv1 = (ListView) findViewById(R.id.listings);

    registerForContextMenu(lv1);
    lv1.setOnItemClickListener(this);

}

还有 onCreateContextMenu:

And the onCreateContextMenu:

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

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
            .getMenuInfo();
    switch (item.getItemId()) {
    case R.id.watch:
        String name = "";
        return true;
    case R.id.buy:
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

我想从列表项中的文本视图中获取文本.我该如何实现?

推荐答案

你应该为上下文菜单注册LISTVIEW.

you should register the LISTVIEW for the context menu.

这是来源.

对于onCreate():

 registerForContextMenu(lv);

并在长按期间访问所选项目:

And to access the selected item during long click:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
   if (v.getId() == R.id.lv) {
       ListView lv = (ListView) v;
       AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
       YourObject obj = (YourObject) lv.getItemAtPosition(acmi.position);

       menu.add("One");
       menu.add("Two");
       menu.add("Three");
       menu.add(obj.name);
   }
}

这篇关于在android中使用带有列表视图的上下文菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 18:46