Adding a string in context menu using for statement

342 views Asked by At

Hi I'm trying to put a string(class prices) in a context menu. But my code is not returning as I expected. Can someone help me to solve this?

public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
         super.onCreateContextMenu(menu, v, menuInfo);  
         menu.setHeaderTitle(getString(R.string.display_title));  
         //menu.add(0, v.getId(), 0, getString(R.string.display_schedule_ticket_prices)); 

         StringBuilder pricesAlert = new StringBuilder("");
            //int i = 0;
         for(float price: rates.getPrices()){
             for (int j = 0; j < 3; j++){
            pricesAlert.append(getString(R.string.display_schedule_train_class) + j + getString(R.string.display_schedule_train_rs) +  price +"\r\n");
            menu.add(pricesAlert);
             }
         } 
     }

Edited:

I'm getting the results something like this using above code. enter image description here

But I want the output like this: enter image description here

1

There are 1 answers

1
Metalhead1247 On

Here is a snippet

 @Override  
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
super.onCreateContextMenu(menu, v, menuInfo);  
menu.setHeaderTitle("Context Menu");  
menu.add(0, v.getId(), 0, "Action 1");  
menu.add(0, v.getId(), 0, "Action 2");  
}  

 @Override  
 public boolean onContextItemSelected(MenuItem item) {  
    if(item.getTitle()=="Action 1"){function1(item.getItemId());}  
else if(item.getTitle()=="Action 2"){function2(item.getItemId());}  
else {return false;}  
return true;  
 }  

Is this what you want to do? refer http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/

have you added actions to your context menu?