它选择的项目(在ListView)催生了文本菜单检测(安卓)菜单、文本、项目、ListView

由网友(旧城里的旧伤)分享简介:我有一个的ListView ,这将允许用户长期preSS的项目以获得上下文菜单。我遇到的问题是确定哪些列表项他们长期pressed。我试着这样做:I have a ListView that will allow the user to long-press an item to get a context menu...

我有一个的ListView ,这将允许用户长期preSS的项目以获得上下文菜单。我遇到的问题是确定哪些列表项他们长期pressed。我试着这样做:

I have a ListView that will allow the user to long-press an item to get a context menu. The problem I'm having is in determining which ListItem they long-pressed. I've tried doing this:

myListView.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
  @Override public void onCreateContextMenu(ContextMenu menu, final View v, ContextMenuInfo menuInfo) {
   menu.add("Make Toast")
    .setOnMenuItemClickListener(new OnMenuItemClickListener() {
     @Override public boolean onMenuItemClick(MenuItem item) {
      String toastText = "You clicked position " + ((ListView)v).getSelectedItemPosition();
      Toast.makeText(DisplayScheduleActivity.this, toastText, Toast.LENGTH_SHORT).show();
      return true;
     }
    });
  } 
 });

但它只是挂起,直到 ANR 弹出。我怀疑是创建菜单后,列表项不再被选中。

but it just hangs until an ANR pops up. I suspect that after the menu is created the ListItem is no longer selected.

看起来你可以监视点击或长时间点击然后记录有点击的项目:

It looks like you could monitor for clicks or long-clicks then record the clicked item there:

 mArrivalsList.setOnItemLongClickListener(new OnItemLongClickListener() {
  @Override public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
   // record position/id/whatever here
   return false;
  }
 });

但感觉majorly kludgey给我。没有人有任何更好的解决方案呢?

but that feels majorly kludgey to me. Does anyone have any better solutions for this?

推荐答案

我做的正是这一点。在我的onCreateContextMenu(...)方法,我投了ContextMenu.ContextMenuInfo到AdapterView.AdapterContextMenuInfo。从那里,你可以得到的TargetView,你再转换为小部件。完整的code是在HomeActivity.java,查找onCreateContextMenu(...)方法。

I do exactly this. In my onCreateContextMenu(...) method, I cast the ContextMenu.ContextMenuInfo to AdapterView.AdapterContextMenuInfo. From there, you can get the targetView, which you cast again to the widget. The complete code is available in HomeActivity.java, look for the onCreateContextMenu(...) method.

@Override
public void onCreateContextMenu(ContextMenu contextMenu,
                                View v,
                                ContextMenu.ContextMenuInfo menuInfo) {
    AdapterView.AdapterContextMenuInfo info =
            (AdapterView.AdapterContextMenuInfo) menuInfo;
    selectedWord = ((TextView) info.targetView).getText().toString();
    selectedWordId = info.id;

    contextMenu.setHeaderTitle(selectedWord);
    contextMenu.add(0, CONTEXT_MENU_EDIT_ITEM, 0, R.string.edit);
    contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);
}

请注意,我店将选定的文本,以及在私人领域选择ID。由于用户界面线程限制,我知道selectedWord和selectedWordId领域将是正确的以后的行动。

Note that I store the selected text as well as the select id in private fields. Since the UI is thread confined, I know the selectedWord and selectedWordId fields will be correct for later actions.

阅读全文

相关推荐

最新文章