I want to display context menu for list position clicked means only for 2 nd position clicked in list view so how to do it.
I had implemented the code for displaying context menu for each position clicked. So how to make it specific for any position in ListView
.
my code for displaying context menu for each position in list view ..pls do required modification on my code th开发者_如何学Pythonanks...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenulist);
// registerForContextMenu(getListView());
CustomAdapter adapter = new CustomAdapter(
this, R.layout.listitem,R.id.title, data
);
setListAdapter(adapter);
getListView().setTextFilterEnabled(true);
}
@Override
public void onCreateContextMenu(ContextMenu menu,
View v,ContextMenuInfo menuInfo)
{
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("Context Menu");
menu.add(0, v.getId(), 0, "Gallery");
menu.add(0, v.getId(), 0, "Camera");
menu.add(0, v.getId(), 0, "Cancel");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Gallery"){
function1(item.getItemId());
} else if(item.getTitle()=="Camera"){
function2(item.getItemId());
} else return false;
return true;
}
public void function1(int id){
Toast.makeText(this, "Gallery function called",
Toast.LENGTH_SHORT)
.show();
}
public void function2(int id){
Toast.makeText(this, "Camera function called",
Toast.LENGTH_SHORT)
.show();
}
In your code I can see only onCreateOptionsMenu() method. In order to restrict the options menu as you wish, you have to add one more method to it.
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
}
And now when you click on the listview get the position of the item that is being clciked and store it globally. Now change the above method like this,
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
super.onPrepareOptionsMenu(menu);
if(list_item_position==2){
return true;
}
else
return false;
}
Now your options menu will pop up only if the list item in second position is clicked.
精彩评论