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