Search code examples
androidcontextmenuexpandablelistviewandroid-viewbinder

Troubles with ContextMenu in ExpandableListActivity


In an ExpandableListActivity I have registered a ContextMenu. What I am trying to do is store the data of the child list item of a group for which the ContextMenu is pressed. According to:

onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo)

v is the view for which the context menu is being built. So this view should be that of the list item that I click but it is not, it is referring to the first list item in the child list. I believe it should return the view of the list item for which the context menu is built but that is not the case here. Here is my code:

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

    }

Can someone shed some light on this?

Edit:

Code for the 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;
        }

    }

The code for the 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;
        }
    };

Solution

  • You could fetch the id of the child from the ContextMenuInfo as well rather than relying on the view. See the documentation for it as it should have what you desire.