在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;
}
};
您可能在回收您的適配器中的列表項時遇到麻煩。你能不能分享一下這些代碼呢?謝謝! – rekaszeru 2011-04-30 11:52:49
這裏是我使用的適配器的代碼。感謝您的幫助 – rogerstone 2011-04-30 12:04:29
你在'viewBinder'中定義了什麼?你可以使用'BaseExpandableListAdapter'實現,它會更清晰 – rekaszeru 2011-04-30 14:34:24