Android listview项目和菜单

Android listview项目和菜单,android,android-listview,android-menu,Android,Android Listview,Android Menu,在我的android应用程序中,我有一个显示用户联系人的列表视图。我需要实现一个选项菜单,它有一个菜单项名Add to Favorites,它将把listview中选定的联系人添加到Favorites列表中 public boolean onContextItemSelected(MenuItem item) { AdapterView.AdapterContextMenuInfo info; try { info = (AdapterView.AdapterContextMen

在我的android应用程序中,我有一个显示用户联系人的列表视图。我需要实现一个选项菜单,它有一个菜单项名Add to Favorites,它将把listview中选定的联系人添加到Favorites列表中

public boolean onContextItemSelected(MenuItem item) {
  AdapterView.AdapterContextMenuInfo info;
  try {
    info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
  }   
  catch (ClassCastException e) { }

  // get the item for which the menu is shown
  Object item = getListAdapter().getItem(info.position);
}

我正在琢磨如何从listview中跟踪所选项目以添加到收藏夹列表。此外,“添加到收藏夹”菜单项应仅在按下“菜单”按钮以及listview中的某个项目已选中时显示。因为页面中还有其他按钮和所有按钮,只有显示这些按钮才能将联系人添加到收藏夹列表。

嗨,BlackRider,我必须使用选项菜单来实现这一点。哦,对不起,我误解了这个问题。请使用上下文菜单。参考此文档:嗨,Vineet Shukla,问题是要满足我对任何使用选项菜单的方法的要求。。我现在一整天都在努力寻找一种方法,通过菜单项跟踪选择要添加到收藏夹列表中的listview项。ThanksI尝试初始化这样的全局变量,boolean focusOnList=false;在onFocusChangeListener中,当hasFocusfocusOnList=true时为true,当无焦点时,focusOnList=false时为true。无论如何,当页面自动加载时,focusOnList值变为true,但listview中未选择任何项目时,您可以将focus listener置于列表之上,并在focus上将所需值保存在本地变量中,然后通过“选项”菜单将其添加到收藏夹列表中。。
public boolean onContextItemSelected(MenuItem item) {
  AdapterView.AdapterContextMenuInfo info;
  try {
    info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
  }   
  catch (ClassCastException e) { }

  // get the item for which the menu is shown
  Object item = getListAdapter().getItem(info.position);
}