1

ExpandableListActivity中,我注册了ContextMenu。我想要做的是存储按下ContextMenu的组的子列表项的数据。 据:ExpandableListActivity中的ContextMenu出现问题

onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) 

v是它的上下文菜单中正在修建的看法。所以这个视图应该是我单击的列表项的视图,但它不是,它指的是子列表中的第一个列表项。我相信它应该返回构建上下文菜单的列表项的视图,但这里不是这种情况。这是我的代码:

public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     menu.setHeaderTitle("My Crumbs"); 

     TextView rowid = (TextView) v 
       .findViewById(R.id.trackdetails_item_row_id); 
     rowId = rowid.getText().toString(); 

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

     // Only create a context menu for the child 
     if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) { 

      TextView trackstats = (TextView) v 
        .findViewById(R.id.trackdetails_item_stats); 
    menu.add(0, MENU_SHARE, 0, "Share on Facebook"); 
     } 

    } 

有人可以对此有所了解吗?

编辑:

代码为ExpandableListAdapter:为ViewBinder

public class MyExpandableListAdapter extends SimpleCursorTreeAdapter { 

     public MyExpandableListAdapter(Cursor cursor, Context context, 
       int groupLayout, int childLayout, String[] groupFrom, 
       int[] groupTo, String[] childrenFrom, int[] childrenTo) { 
      super(context, cursor, groupLayout, groupFrom, groupTo, 
        childLayout, childrenFrom, childrenTo); 
      setViewBinder(viewBinder); 
     } 

     @Override 
     protected Cursor getChildrenCursor(Cursor groupCursor) { 
      // TODO Auto-generated method stub 
      String crumbName = groupCursor.getString(mCrumbNameColumnIndex); 
      return crumpareDBAdapter.getTrackList(mTracksProjection, crumbName); 
     } 

     @Override 
     public SimpleCursorTreeAdapter.ViewBinder getViewBinder() { 
      return viewBinder; 
     } 

    } 

代码:

SimpleCursorTreeAdapter.ViewBinder viewBinder = new ViewBinder() { 

     @Override 
     public boolean setViewValue(View view, Cursor cursor, int columnIndex) { 
      // TODO Auto-generated method stub 
     TextView textView = (TextView) view; 
     textView.setText(cursor.getString(columnIndex)); 
      return true; 
     } 
    }; 
+0

您可能在回收您的适配器中的列表项时遇到麻烦。你能不能分享一下这些代码呢?谢谢! – rekaszeru 2011-04-30 11:52:49

+0

这里是我使用的适配器的代码。感谢您的帮助 – rogerstone 2011-04-30 12:04:29

+0

你在'viewBinder'中定义了什么?你可以使用'BaseExpandableListAdapter'实现,它会更清晰 – rekaszeru 2011-04-30 14:34:24

回答

1

你可以获取从ContextMenuInfo孩子的ID以及而不是依靠这个观点。请参阅documentation,因为它应该有你想要的。

+0

我已经得到了我想要的东西。我想知道这个方法的确切错误 – rogerstone 2011-04-30 12:58:20

+0

我正在使用getExpandableListView.getChildAt(index)[where index = childpos]来获得相应的视图,但它不会给出我认为它总是给我我想要的东西,但我想并不总是:)。id正是我应该使用的。而且contextMenuinfo中的targetview为您提供了contextmenu所处的视图这些都证明对我来说比较方便。非常感谢。 – rogerstone 2011-05-06 10:24:44

+1

如果您使用标准机制_inflating_可扩展列表中的视图,那很可能是问题所在。他们只会首次夸大列表中的观点,并重新使用后续项目的观点。因此,你提取的视图几乎肯定不会包含你想要的数据:)。 – 2011-05-06 19:50:27