2013-12-19 31 views
2

我應該使用自定義線創建自定義彈出式菜單,這就是爲什麼我使用自定義對象創建了數組列表,因爲每行必須包含標題和副標題。當我必須把這些物品,在彈出的我得到這個錯誤:自定義數組列表彈出式菜單

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; 

} 
} 

回答

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

相反的mPopupList[0],嘗試mPopupList.get(0)。在內部,ArrayList使用Array來存儲其數據,但ArrayList類是List,其中有get(index)方法來訪問其數據。

+0

我得到這樣的錯誤**類型菜單中Add方法(INT,INT,INT,爲CharSequence)不適用於參數(int,int,int, PopupItem)** –

+0

最後一個參數需要是一個CharSequence,所以你的項目的文本表示(你想在菜單中看到的文本)。有一個多行菜單項是不是真的可能 –

+0

我添加一個方法getString()在PopupItem.java爲concate title和subtitle。我的解決方案是 –

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

insted的的

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

,因爲它是一個數組列表。

+0

我得到像錯誤**我thod add(int,int,int,CharSequence)在Menu類型中不適用於參數(int,int,int, PopupItem)** –

0

菜單項是在ArrayList中,下面的代碼將幫助ü:)

限制是一個ArrayList;

Dynamic_PopUpMenu.setOnClickListener(新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(); 
     } 
    });