从上下文操作栏访问ListView项的文本 [英] Accessing a ListView item's text from Contextual Action Bar

查看:147
本文介绍了从上下文操作栏访问ListView项的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要通过长按选择访问的ListView项的文本。对于旧的Andr​​oid版本,我已经成功地与下面的code上下文菜单做到了这一点。

I need to access the text in a ListView item through a long click selection. For old Android versions I have successfully done this with context menus with the code below.

@Override
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();
    String text = ((TextView) info.targetView).getText().toString();

    switch (item.getItemId()) {
        case R.id.getText:
            getText(text);
            return true;
        default:
            return super.onContextItemSelected(item);
    }
}

对于较新的Andr​​oid版本,不过,我想与上下文操作栏做到这一点,但无法弄清楚如何在选择在酒吧的项目后,提取选定的文本。下面code不起作用。

For newer Android versions, however, I would like to do this with the Contextual Action Bar but can't figure out how to extract the selected text after having selected an item in the bar. The below code does not work.

    myListView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    myListView.setMultiChoiceModeListener(new MultiChoiceModeListener() {

        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
            AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
        String text = ((TextView) info.targetView).getText().toString();

            switch (item.getItemId()) {
                case R.id.contextDelete:
                    getText(text);
                    return true;;
                default:
                    return false;
            }
        }

        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
            MenuInflater inflater = mode.getMenuInflater();
            inflater.inflate(R.menu.context_menu, menu);
            return true;
        }

        //Other actionmode methods...
    });

上下文动作栏显示出来好的,但是从它挑选项目时,我得到一个NullPointerException异常的AdapterContextMenuInfo线,因为这对上下文菜单,而不是动作条明显进行。有一些相当于该行动吧吧?或者我怎样才能在这种情况下,ListView项的文本?谢谢你。

The Contextual Action Bar shows up ok, but when picking an item from it I get a NullPointerException at the AdapterContextMenuInfo line, since this was obviously made for context menus and not action bars. Is there some equivalent for this for Action bars perhaps? Or how can I get the ListView item text in this case? Thanks.

推荐答案

getCheckedItemPositions()的ListView 返回该项目定位的用户已经检查过了, getCheckedItemIds()会,如果你使用的是类似的CursorAdapter 返回自己的ID值

getCheckedItemPositions() on ListView will return the item positions the user has checked, and getCheckedItemIds() will return their ID values if you are using something like CursorAdapter.

下面是一个示例项目展示了如何使用的CHOICE_MODE_MULTIPLE_MODAL 在API级别11+和旧设备回落至上下文菜单。

Here is a sample project demonstrating the use of CHOICE_MODE_MULTIPLE_MODAL on API Level 11+ and falling back to context menus on older devices.

这篇关于从上下文操作栏访问ListView项的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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