【问题标题】:Adding a string in context menu using for statement使用 for 语句在上下文菜单中添加字符串
【发布时间】:2013-09-14 15:20:38
【问题描述】:

您好,我正在尝试将字符串(课程价格)放在上下文菜单中。但是我的代码没有按预期返回。有人可以帮我解决这个问题吗?

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

已编辑:

我使用上面的代码得到了类似这样的结果。

但我想要这样的输出:

【问题讨论】:

  • 上下文菜单出现了吗?它在显示什么?
  • 是的,它在上下文菜单中显示 1,2 和 3 类的价格列表。但是我循环的方式是错误的。
  • 我没听懂你,你是说选择一个选项后什么都没有发生?
  • 请描述预期的结果。您的问题不清楚。
  • @Metalhead1247 & Chiral Code:我编辑了我的帖子。请看上面。而rates.getPrices() 是调用价格的对象。

标签: android android-layout android-listview android-context android-contextmenu


【解决方案1】:

这是一个sn-p

 @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;  
 }  

这是你想做的吗?参考http://www.stealthcopter.com/blog/2010/04/android-context-menu-example-on-long-press-gridview/

您是否在上下文菜单中添加了操作?

【讨论】:

  • 是的。我已经检查了那个链接。但我想添加一个 json 对象作为上下文菜单列表。 rates.getPrices()
猜你喜欢
  • 2018-07-06
  • 2012-12-21
  • 1970-01-01
  • 2023-02-10
  • 2021-01-28
  • 2019-08-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多