Question

I'm working on a project that has list of ice cream flavors. When you long-click on a flavor you see a context menu with some options : picture, ingredients, price ect. If chocolate is long-clicked and you select picture, you should see a picture of chocolate ice cream...if you did the same for vanilla, you'd see vanilla ice cream.

Here's the problem. The selection you make within the context menu is related to the original flavor you long-clicked. I need to be able to access the position of the flavor within the flavor array. -- in the OnListItemClicked() method the position of my flavors array is accessible, however...the OnContextItemSelect() method is only aware of it's own selection items (picture,ingredient, price ).

You can see I had created a listPosition variable, but I've realized that this would only work if a flavor was clicked quickly using the OnListItemClicked() as opposed to long-clicked using OnContextItemSelect().

the ApplyMenuChoice() is where the problem code can be found.

I would be so grateful for help, Thanks :)

MenuDemo.java

public class MenuDemo extends ListActivity {
    
    TextView selection;
    TextView displayText;
    String[] flavorItems;
    String[] colors;
    ImageView myImage;
    int currentMenu;
    int listPosition = 0;
    
    int[] ItemImageSource  = {R.drawable.vanilla,R.drawable.chocolate,R.drawable.strawberry};

    private final String TAG = "Main Activity";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        selection=(TextView)findViewById(R.id.selection);
        displayText=(TextView)findViewById(R.id.displayText);
        myImage=(ImageView)findViewById(R.id.myImage);
        flavorItems = getResources().getStringArray(R.array.flavors);
        colors = getResources().getStringArray(R.array.colors);
        
        
        
        setListAdapter(new ArrayAdapter<String>(this, R.layout.flavor_menu_style, flavorItems));
        registerForContextMenu(getListView());
        displayText.setText("Welcome! Click and hold flavor to learn more about it.");
    }
    
    public void onListItemClick(ListView parent, View v,int position, long id) 
    {   
        
        if (currentMenu == 1){selection.setText(colors[position]);}
        else {selection.setText(flavorItems[position]);}
        
        String strI = Integer.toString(position);
        displayText.setText("teeesst" + strI);
        
        listPosition = position;
        Log.i(TAG,"!!!!!!!!!!!!!!");
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v,ContextMenu.ContextMenuInfo menuInfo) 
    {
        super.onCreateContextMenu(menu, v, menuInfo);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_menu_demo, menu);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {       
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.activity_new, menu);
        
        
        return true;    
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) { //item is the menu item chosen
        
        boolean ret = applyOptionMenuChoice(item);
        if ( ret == false)
            ret = super.onOptionsItemSelected(item);
        
        return ret;
            
    }
    
    
    @Override
    public boolean onContextItemSelected(MenuItem item) 
    {
        boolean ret = applyMenuChoice(item);
        if ( ret == false)
            ret = super.onContextItemSelected(item);
        
        return ret;
    }
    
     
    private boolean applyMenuChoice(MenuItem item) {
        switch (item.getItemId()) 
        {
        case R.id.picture:

        //this would work if listPosition was linked to the flavors array position
            myImage.setImageResource(ItemImageSource[listPosition]);

            return(true);
            
        case R.id.ingredients:
            
            String [] ingredients;
            ingredients = new String [3];
            
            ingredients[0] = "VANILLA -- these are the ingredients VANILLA";
            ingredients[1] = "CHOCOLATE -- these are the ingredients";
            ingredients[2] = "STRAWBERRY -- these are the ingredients";
            
            displayText.setText(ingredients[listPosition]);
            
            return(true);
            
        case R.id.order:
            //I will add stuff here later
            return(true);
        
        case R.id.price:
            //I will add stuff here later
            return(true);
        
            
        
        }

        return(false);
        
    }
    
    private boolean applyOptionMenuChoice(MenuItem item) {
        switch (item.getItemId()) 
        {
        case R.id.color_menu:
            setListAdapter(new ArrayAdapter<String>(this, R.layout.color_menu_style, colors));
            currentMenu = 1;
            return(true);
            
        case R.id.flavor_menu:
            
            setListAdapter(new ArrayAdapter<String>(this, R.layout.flavor_menu_style, flavorItems));
            currentMenu = 2;
            return(true);
        
        }

        return(false);
        
    }
    
    
}
Was it helpful?

Solution

You can get the position of the list item you long-pressed via the MenuItem

    @Override
    public boolean onContextItemSelected(MenuItem item) {
         AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
         int listPosition = info.position;
         //pass this to your applyMenuChoice method. ice cream nom nom nom
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top