1

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;
        }
    };
user
  • 86,916
  • 18
  • 197
  • 190
rogerstone
  • 7,541
  • 11
  • 53
  • 62

1 Answers1

1

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.

Brian Dupuis
  • 8,136
  • 3
  • 25
  • 29
  • I have got what i wanted.I wanted to know what is exactly wrong with this method – rogerstone Apr 30 '11 at 12:58
  • I was using getExpandableListView.getChildAt(index)[where index=childpos] to get the corresponding view but it din't give me right view always.I thought it was giving me what i wanted but i guess not always :).The id was exactly what i should have been using.Moreover the targetview from the contextMenuinfo gives you the the view for which the contextmenu is being built.Both of these have proved to be more than handy to me.Thanks a lot. – rogerstone May 06 '11 at 10:24
  • 1
    If you're using the standard mechanism for _inflating_ the views in the expandable list, that's likely the problem. They will only inflate the views in the list the very first time and reuse the views for subsequent items. Therefore, the view that you fetch will almost certainly not contain the data that you desire :). – Brian Dupuis May 06 '11 at 19:50