TheLettuceMaster
TheLettuceMaster

Reputation: 15734

How to make a conditional list item in a Android ContextMenu

I have a context menu on a ListView.

I would like to add a special-case context list item for logged in users.

Let's say there is a list of comments. But ONLY for the logged in user's comments, there is a special context list-item called "Edit" (Obviously you don't want other users to be able to edit comments outside of their own.

Here is my class extending application in which I usually check in for logged in users:

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);
    }

}

Here is my context menu:

@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;

        }

Upvotes: 0

Views: 784

Answers (2)

Trung Nguyen
Trung Nguyen

Reputation: 7532

Simple you add your Edit item depend on user login status in 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);



        }

Upvotes: 2

dafi
dafi

Reputation: 3522

You can cast ContextMenuInfo to AdapterView.AdapterContextMenuInfo for ListView than change its content accessing to adapter (your own implementation).

AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo)menuInfo;
        MyAdapter adapter = (MyAdapter)getListView().getAdapter();
adapter.remove(contextMenuInfo.position);

Upvotes: 0

Related Questions