Pregunta

I should create a custom popup menu with a custom lines, which is why I created a arraylist with custom objects because each line must include a title and a subtitle. when I have to put these items in the popup I get this error:

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

My code:

    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

in 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;

}
}
¿Fue útil?

Solución

public class PopupItem 
{

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

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


    return string;
  }
}

In 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() );

Otros consejos

Instead of mPopupList[0], try mPopupList.get(0). Internally, an ArrayList uses an Array to store its data, but the ArrayList class is a List, which has the get(index) method to access its data.

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

insted of

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

since it is an array list.

Menu items are in ArrayList, the underneath Code will help u:)

limits is an 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();
        }
    });
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top