在ExpandableListActivity中使用ContextMenu进行故障

时间:2011-04-30 11:47:05

标签: android contextmenu expandablelistview android-viewbinder

ExpandableListActivity我注册了ContextMenu。我要做的是存储按下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的代码:

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;
        }

    }

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;
        }
    };

1 个答案:

答案 0 :(得分:1)

您也可以从ContextMenuInfo中获取子项的id,而不是依赖于视图。请参阅documentation,因为它应该符合您的要求。