【问题标题】:Make Static menu to dynamic from arraylist in android将静态菜单从android中的arraylist变为动态
【发布时间】:2017-03-23 10:54:35
【问题描述】:

我有一个 popup_menu.xml,它有 3 个静态项目标签,因此这 3 个项目值显示在弹出菜单中,但我有一个数组列表,其中有几个值我想在该弹出菜单中显示。

我只是想显示在 markersArray 中可用的汽车名称,而不是在 popup_menu.xml 中可用的静态项目值

Array List array 


for(int i=0; i<markersArray.size(); i++){
            String caname = markersArray.get(i).getCarname();
    }

        popup_menu.xml
        <?xml version="1.0" encoding="utf-8"?>
        <menu xmlns:android="http://schemas.android.com/apk/res/android">
            <item
                android:id="@+id/one"
                android:title="One"/>

            <item
                android:id="@+id/two"
                android:title="Two"/>

            <item
                android:id="@+id/three"
                android:title="Three"/>
        </menu>

        MainActivity.java
         @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);

           final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
               @Override
               public void onClick(View view) {
                // Click action
                System.out.println("Float Icon Clicked");
        //Creating the instance of PopupMenu
                PopupMenu popup = new PopupMenu(MainActivity.this, fab);
         //Inflating the Popup using xml file
            popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());

          //registering popup with OnMenuItemClickListener
          popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
          public boolean onMenuItemClick(MenuItem item) {
              Toast.makeText(MainActivity.this, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
                                return true;
                            }
                        });

                        popup.show();//showing popup menu

                    }
                });
            }

【问题讨论】:

    标签: java android android-layout listview menuitem


    【解决方案1】:

    这行代码下

     PopupMenu popup = new PopupMenu(MainActivity.this, fab);
    

    只需遍历您的 ArrayList 并将其添加到弹出菜单中,如下所示

    for (String s : array) {
            popup.getMenu().add(s);
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-05-04
      • 2013-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-14
      • 1970-01-01
      相关资源
      最近更新 更多