views:

82

answers:

2

I am having a lot of trouble understanding how the Android context menu works. I am writing a simple program which will display a list of different types of ice cream in a list view. When the user to "taps and holds over each type, a context menu should appear with 4 options asking what information they want to be displayed about that ice cream (picture, ingredients, order, other).

I don't understand how to make it so that each list item's context menu gives the appropriate info (the info associated with whichever list item the user tapped and held).

I am working from an example given by my teacher, which has some of the functionality I am after, but does not deal with the issue of displaying different info for each list item. I see that in the given code, my prof even gave a hint in the comments within the parameters of onCreateContextMenu():

public void onCreateContextMenu( //called each time the context menu is requested
        ContextMenu menu,   //the ContextMenu itself
        View v, //The view for which the context menu is being built
        ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

But I am still unsure of what I am able to do with this parameter, and I've been spinning my wheels for a long time now. There is at least one other thread on stackoverflow that addresses this question, but it did not help me get any closer to understanding what's going on here.

I really appreciate the help, as I am new to Android (and OOP generally) and a little hesitant to ask questions for fear of public techie ridicule or being seen as taking advantage of the community. I just hope that at some point I'll be able to pay it forward to someone else.

Thanks!

Here's my code:

package CS285.Assignment2;

import android.app.Activity;
import android.os.Bundle;
import android.app.ListActivity;
import android.util.Log;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;


public class IceCreamMenu extends ListActivity {
    TextView selection;
    ImageView imgView;

    String[] items={"Butter Pecan", "Chocolate", 
            "Coffee", "Mango", 
            "Rocky Road","Strawberry", 
            "Vanilla"};

    int[] images={R.drawable.butterpecan,
            R.drawable.choclate,
            R.drawable.coffee,
            R.drawable.mango,
            R.drawable.rockyroad,
            R.drawable.strawberry,
            R.drawable.vanilla};

    public static final int IMG_ID = 0;
    public static final int INGR_ID = 1;
    public static final int ORDER_ID = 2;
    public static final int OTHER_ID = 3;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        selection=(TextView)findViewById(R.id.selection);
        imgView = (ImageView)findViewById(R.id.Image);

        setListAdapter(new ArrayAdapter<String>(this,
                                                android.R.layout.simple_list_item_1, 
                                                items));
        registerForContextMenu(getListView()); //indicate that the ListView has a context menu.

    }

    //event handling for ListItemClick events
    public void onListItemClick(ListView parent, View v,
                        int position, long id) {

        selection.setText(items[position]);

    }

    @Override
    public void onCreateContextMenu( //called each time the context menu is requested
            ContextMenu menu,   //the ContextMenu itself
            View v, //The view for which the context menu is being built
            ContextMenu.ContextMenuInfo menuInfo) { //tells you which item in the list the user did the tap-and-hold over 

            populateMenu(menu);

    }

    private void populateMenu(Menu menu) {
        menu.add(Menu.NONE, IMG_ID, Menu.NONE, "Picture");
        menu.add(Menu.NONE, INGR_ID, Menu.NONE, "Ingredients");
        menu.add(Menu.NONE, ORDER_ID, Menu.NONE, "Order");
        menu.add(Menu.NONE, OTHER_ID, Menu.NONE, "Other");

    }

    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) {
        case IMG_ID:

            imgView.setImageResource(images[0]);

            return(true);

        case INGR_ID:


            return(true);

        case ORDER_ID:


            return(true);

        case OTHER_ID:


            return(true);
        }

        return(false);
    }

    //called whenever an item in a ContextMenu is selected.
    @Override
    public boolean onContextItemSelected(MenuItem item) { //item is the menu item chosen
        return(applyMenuChoice(item) ||  
        super.onContextItemSelected(item));
    }


}
A: 

You can't do tap and hold functionality on a context menu. It sounds like what you want is a dialog.

Falmarri
A: 

The short answer is cast menuInfo to an AdapterContextMenuInfo object and then access its public position member variable

selectedPostion = ((AdapterView.AdapterContextMenuInfo)menuInfo).position;

The value of selectedPostion is the position in the ListView that was long-clicked.

This article in the Android Developer Guide has an excellent description of how to work with the ContextMenuInfo object, a must read:

http://developer.android.com/guide/topics/ui/menus.html#context-menu

As you can see in your onCreate() method your ListView is registering itself to respond to long-clicks and generate a ContextMenu. So when you long-click on a View item in the ListView, it is the ListView that is providing your onCreateContextMenu method with the menuInfo parameter. This parameter contains information about how the ContextMenu was generated.

http://developer.android.com/reference/android/view/ContextMenu.ContextMenuInfo.html

The ListView extends the AdapterView class. Which has AdpterView.AdapterContextMenuInfo subclass. Whenever the ListView object is long clicked, it sets up this parameter with information about what View object was long clicked. This object then gets passed to the onCreateContextMenu(...) method.

http://developer.android.com/reference/android/widget/AdapterView.AdapterContextMenuInfo.html

James