从 android 中的数组列表将静态菜单变为动态菜单
Make Static menu to dynamic from arraylist in android
我有一个 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
}
});
}
在这行代码下
PopupMenu popup = new PopupMenu(MainActivity.this, fab);
只需遍历您的 ArrayList 并将其添加到弹出菜单中,就像这样
for (String s : array) {
popup.getMenu().add(s);
}
我有一个 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
}
});
}
在这行代码下
PopupMenu popup = new PopupMenu(MainActivity.this, fab);
只需遍历您的 ArrayList 并将其添加到弹出菜单中,就像这样
for (String s : array) {
popup.getMenu().add(s);
}