【问题标题】:How can I delete the item that selected by context menu on Expandable Listview on Android?如何删除在 Android 上的 Expandable Listview 上通过上下文菜单选择的项目?
【发布时间】:2011-11-30 16:42:32
【问题描述】:

我有这个代码:

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
  super.onCreateContextMenu(menu, v, menuInfo);

  ExpandableListView.ExpandableListContextMenuInfo info =
      (ExpandableListView.ExpandableListContextMenuInfo) menuInfo;
  int type =
      ExpandableListView.getPackedPositionType(info.packedPosition);

      int group =
      ExpandableListView.getPackedPositionGroup(info.packedPosition);

      int child =
      ExpandableListView.getPackedPositionChild(info.packedPosition);

      //Only create a context menu for child items

      if (type == 1) {
      //menu.setHeaderTitle("Action");
      menu.add(0, MENU_EDIT, 0, "Edit");
      menu.add(0, MENU_DELETE, 1, "Delete");
      }
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo) item.getMenuInfo();
    AdapterContextMenuInfo info1 = (AdapterContextMenuInfo) item.getMenuInfo();

        int groupPos = 0, childPos = 0;

        int type = ExpandableListView.getPackedPositionType(info.packedPosition);

        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
        groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
        childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
        }

        return true;

  switch (item.getItemId()) {
  case MENU_DELETE:

    return true;
  }

  return(super.onOptionsItemSelected(item));
}

我想删除项目,如何填写case MENU_DELETE: 的代码?这是可扩展列表视图和上下文菜单的组合,我想通过上下文菜单删除子项。感谢您的帮助和解释

【问题讨论】:

标签: android contextmenu hashmap expandablelistview


【解决方案1】:

看看这个不错的东西Android Context Menu

@Override
 public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item
        .getMenuInfo();

    switch (item.getItemId()) {
      case R.id.remove_item:
       quoteResult.remove(info.position);
       ((StockQuoteAdapter)getListAdapter()).notifyDataSetChanged();
      return true;
     }
  return false;
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-24
    • 1970-01-01
    • 2023-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多