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.
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...
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With