我有listview: 如下图所示,当用户按下向下箭头选项菜单时会弹出 会有多个帖子(如Facebook),选项菜单弹出窗口有不同的选项 如果用户是该帖子的所有者,那么菜单选项将是编辑帖子,删除帖子 如果用户不是所有者而不是菜单选项将是垃圾邮件 我有2个菜单。 如果用户不是该帖子的所有者,那么它不能按照要求工作,而且还有编辑/删除的弹出菜单打开
poupup_menu:
<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=".activity.HomeActivity" >
<item
app:showAsAction="never"
android:id="@+id/one"
android:title="Edit" />
<item
android:id="@+id/two"
android:title="Delete"/>
</menu>
popup_menu_user:
<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=".activity.HomeActivity" >
<item
android:id="@+id/three"
android:title="Spam"/>
</menu>
我在Listview适配器中为获取特定位置做了什么:
Listview适配器:
ivOptionMenu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View tempView = (View) v.getTag(R.integer.btnplusview);
pos = (Integer) v.getTag(R.integer.btnpluspos);
tvPostDetail = (TextView) tempView.findViewById(R.id.tvPostDetail);
positionItemFeed = String.valueOf(pos);
if (post_user_id.equals(preferenceHelper.getID())) {
PopupMenu popup = new PopupMenu(ctx, v);
popup.getMenuInflater().inflate(R.menu.poupup_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
String menuOption;
menuOption = (String) item.getTitle();
if (menuOption.equals("Edit")) {
editPost();
} else {
delete();
}
return true;
}
});
popup.show();//showing popupuser menu
} else {
PopupMenu popup = new PopupMenu(ctx, v);
popup.getMenuInflater().inflate(R.menu.popup_menu_user, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
public boolean onMenuItemClick(MenuItem item) {
// Toast.makeText(ctx, "You Clicked : " + item.getTitle(), Toast.LENGTH_SHORT).show();
String menuOption;
menuOption = (String) item.getTitle();
if (menuOption.equals("Spam")) {
LayoutInflater li = LayoutInflater.from(ctx);
View promptsView = li.inflate(R.layout.prompts, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
ctx);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
final EditText userInput = (EditText) promptsView
.findViewById(R.id.editTextDialogUserInput);
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// get user input and set it to result
// edit text
reasonSpam = userInput.getText().toString();
reportSpam(reasonSpam);
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
return true;
}
});
popup.show();//showing popupuser menu
}
}
});
答案 0 :(得分:0)
我在比较中:
if (post_user_id.equals(preferenceHelper.getID()))
,这里就是post_user_id没有得到,所以菜单没有弹出正确
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.one:
editPost();
return true;
case R.id.two:
delete();
return true;
case R.id.three:
/* reasonSpam = userInput.getText().toString();
reportSpam(reasonSpam);*/
return true;
}
return true;
}
});
/** Showing the popupuser menu */
popup.show();