Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to disable the context menu for particular ListView items in Android

I know this question was asked previously and the reply was to override onPrepareContextMenu()/onCreateContextMenu(). But I didnt understand and didnt get any solution for it. Please help me how to disable the context menu for particular ListView items.

like image 484
Dileep Perla Avatar asked Mar 18 '12 16:03

Dileep Perla


2 Answers

Opening your context menu depends on your some logic. For example, in method onItemClick (in your listView) you should check content of your item and show or don't show context menu. I don't understand, why it's problem for you?

UPDATE

public class ExampleActivity extends ListActivity {

    private ListView mListView;
    private ArrayList<String> mList = new ArrayList<String>();
    private ArrayAdapter<String> mArrayAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mArrayAdapter = new ArrayAdapter<String>(this, android.R.id.list ,mList);
        mListView = (ListView) findViewById(android.R.id.list);
        setListAdapter(mArrayAdapter);
        registerForContextMenu(mListView);

        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {

            @Override
            public boolean onItemLongClick(AdapterView<?> adapterView , View v,int position, long id) {
                mListView.getItemAtPosition(position); //check current item with your logic and show or don't show contextMenu
                // for example I will show
                mListView.showContextMenu(); //to show
                return true;
            }
        });
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        // add contextmenu items
        super.onCreateContextMenu(menu, v, menuInfo);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        // todo some logic...
        return super.onContextItemSelected(item);
    }

}

May be it will help you... good luck...

like image 164
Ilya Demidov Avatar answered Nov 17 '22 15:11

Ilya Demidov


Also you can re-register your listview which will cause your onCreateContextMenu method to execute again

ListView lv = (ListView) findViewById(R.id.lvExample);
registerForContextMenu(lv);

And then you can hide or display menu items with which ever logic you want in the onCreateContextMenu method

like image 2
Zion115 Avatar answered Nov 17 '22 15:11

Zion115