Android 如何通过单击菜单项获取下拉菜单项?
我在自定义ActionBar中实现下拉列表(如微调器)时遇到问题。我尝试了几种解决方案,包括getActionView(),但都不起作用:/Android 如何通过单击菜单项获取下拉菜单项?,android,drop-down-menu,android-actionbar,Android,Drop Down Menu,Android Actionbar,我在自定义ActionBar中实现下拉列表(如微调器)时遇到问题。我尝试了几种解决方案,包括getActionView(),但都不起作用:/ Here is my code; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.me
Here is my code; @Override public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.planner_menu, menu);
MenuItem category_item = menu.findItem(R.id.CategoryAppointment);
Spinner spinner =(Spinner) category_item.getActionView();
String[] categories = new String[]{"meeting","training","puls","medicin","bloodsample"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_dropdown_item,categories);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
parent.setSelection(position);
switch (position) {
case 0:
String category = parent.getSelectedItem().toString();
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + category, Toast.LENGTH_SHORT).show();
// Take string and compare it in database.
DB.getAllAppointmentsByCategory(category);
break;
case 1:
// Take string and compare it in database.
break;
case 2:
// Take string and compare it in database.
break;
case 3:
// Take string and compare it in database.
break;
case 4:
// Take string and compare it in database.
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
for (int j = 0; j < menu.size(); j++) {
MenuItem item = menu.getItem(j);
Log.d("TAG", "set flag for " + item.getTitle());
item.setShowAsActionFlags(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
return true;}
XML文件中的代码
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Planning">
<item android:id="@+id/AllAppointments" android:title="All"
android:orderInCategory="10"
app:showAsAction="always|withText">
</item>
<item android:id="@+id/WeekAppointment"
android:title="Week"
android:orderInCategory="10"
app:showAsAction="always|withText"
>
</item>
<item android:id="@+id/CategoryAppointment"
android:title="Cathegories"
android:orderInCategory="10"
app:showAsAction="always|withText"
>
</item>
`
在item id->CategoryAppoition中,我想显示一个下拉菜单,用户可以在其中选择不同的类别,然后从数据库中列出数据。
在过去的几个小时里,我一直在研究这个问题,但似乎无法解决它。有人能给我解释一下我必须做什么以及我做错了什么吗
多谢各位
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" tools:context=".Planning">
<item android:id="@+id/AllAppointments" android:title="All"
android:orderInCategory="10"
app:showAsAction="always|withText">
</item>
<item android:id="@+id/WeekAppointment"
android:title="Week"
android:orderInCategory="10"
app:showAsAction="always|withText"
>
</item>
<item android:id="@+id/CategoryAppointment"
android:title="Cathegories"
android:orderInCategory="10"
app:showAsAction="always|withText"
>
</item>