带自定义arraylist的弹出菜单

时间:2013-12-19 10:36:19

标签: android arraylist popupmenu

我应该创建一个带有自定义行的自定义弹出菜单,这就是我创建带有自定义对象的arraylist的原因,因为每行必须包含标题和副标题。当我必须将这些项目放入弹出窗口时,我收到此错误:

The type of the expression must be an array type but it resolved to 
 ArrayList<PopupItem>

我的代码:

    navMenuOverflowTitles =new String[]{"Text","Dashboard","Settings", "Order","Filter"};
navMenuOverflowSubTitles = new String[]{"standard","Recently", "","",""};
mPopupList = new ArrayList<PopupItem>();

mPopupList.add(new PopupItem(1,navMenuOverflowTitles[0], navMenuOverflowSubTitles[0], "item1"));
mPopupList.add(new PopupItem(2,navMenuOverflowTitles[1], navMenuOverflowSubTitles[1], "item2"));
mPopupList.add(new PopupItem(3,navMenuOverflowTitles[2], navMenuOverflowSubTitles[2], "item3"));
mPopupList.add(new PopupItem(4,navMenuOverflowTitles[3], navMenuOverflowSubTitles[3], "item4"));
mPopupList.add(new PopupItem(5,navMenuOverflowTitles[4], navMenuOverflowSubTitles[4], "item5"));
View menuItemView = findViewById(R.id.menu_overflow);

popupMenu = new PopupMenu(this, menuItemView); 

popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, mPopupList[0] );//the error is here
PopupItem.java中的

  public class PopupItem 
  {
  private int itemId;
  private String subtitleText;
  private String titleText;
  private String tag;



public PopupItem( int itemId, String title, String subtitle, String tag) {
    this.itemId=itemId;
    this.subtitleText=subtitle;
    this.titleText=title;
    this.tag = tag;

}
}

4 个答案:

答案 0 :(得分:1)

而不是mPopupList[0],请尝试mPopupList.get(0)。在内部,ArrayList使用Array来存储其数据,但ArrayList类是List,其get(index)方法可以访问其数据。

答案 1 :(得分:1)

popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, mPopupList.get(0))

而不是

popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, mPopupList[0])

因为它是一个数组列表。

答案 2 :(得分:1)

public class PopupItem 
{

  //.....
  public String getString() {

    String string =  new String(this.titleText +" "+ this.subtitleText);


    return string;
  }
}

在mainActivity.java中

  popupMenu = new PopupMenu(this, menuItemView); 

  popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, mPopupList.get(0).getString() );
  popupMenu.getMenu().add(Menu.NONE, 1, Menu.NONE, mPopupList.get(1).getString() );

答案 3 :(得分:0)

菜单项位于ArrayList中,代码下方将帮助您:)

limits是一个ArrayList;

Dynamic_PopUpMenu.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            PopupMenu menu = new PopupMenu(DialogCheckBox.this, v);
            for (String s : limits)
            {
                menu.getMenu().add(s);
            }
            menu.show();
        }
    });