我在ListView上有一个上下文菜单。
我想为登录用户添加特殊情况上下文列表项。
假设有一个评论列表。但是,仅对于登录用户的评论,有一个特殊的上下文列表项称为“编辑”(显然,您不希望其他用户能够编辑自己的评论。
这是我的类扩展应用程序,我通常会为登录用户办理登机手续:
public class MyApp extends Application {
public static boolean isUserLoggedIn = false;
public static String username = null;
public static SharedPreferences logInState;
public static int ratescreen = 0;
public static boolean userLogin() {
return MyApp.isUserLoggedIn = true;
}
public static boolean userLogout() {
return MyApp.isUserLoggedIn = false;
}
public static void setUser(String s) {
MyApp.username = s;
}
@Override
public void onCreate() {
super.onCreate();
String PREFS_NAME = "LoginState";
logInState = getSharedPreferences(PREFS_NAME,
MODE_PRIVATE);
}
}
这是我的上下文菜单:
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.reviews_context, menu);
menu.setHeaderTitle("Mark Comment as ...");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.helpful:
new HelpfulTask().execute();
Toast.makeText(getActivity(), "You have voted this up!",
Toast.LENGTH_SHORT).show();
return true;
case R.id.unhelpful:
new UnHelpfulTask().execute();
Toast.makeText(getActivity(), "You have voted this down!",
Toast.LENGTH_SHORT).show();
return true;
case R.id.spam:
new SpamTask().execute();
Toast.makeText(getActivity(),
"You have reported this as Spam or Offensive.",
Toast.LENGTH_SHORT).show();
return true;
// Would like to add fourth option here but conditional if it is a comment from the currently logged in user.
}
return false;
}
答案 0 :(得分:2)
简单地添加Edit
项取决于onCreateContextMenu
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
// TODO Auto-generated method stub
super.onCreateContextMenu(menu, v, menuInfo);
// check if user logged-in so add Edit item to context menu.
if (userLogin()) {
menu.add(0, MENU_ITEM_EDIT, 0, R.string.menu_edit);
}
//Add normall others menu items
menu.add(0, MENU_ITEM_CALL, 0, R.string.menu_callContact);
}
答案 1 :(得分:0)
您可以将ContextMenuInfo转换为适用于ListView的AdapterView.AdapterContextMenuInfo,而不是更改其内容访问适配器(您自己的实现)。
AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo)menuInfo;
MyAdapter adapter = (MyAdapter)getListView().getAdapter();
adapter.remove(contextMenuInfo.position);