Reputation: 667
Lets say I have a list of friends stored in a database and showed in a list in my activity. For this list, I have an context menu with "edit", "delete" and "bad friend/good friend". The case is that I would like to change the text of the last item in the menu ("bad friend/good friend") based on a value in the database. (Toggle the text).
If the friend is a good friend, the context menu text should be "not good friend" and if the friend is a bad friend, the text should be "good friend". This means that a click on this item in the context menu toggles the friends from good to bad or bad to good.
Any suggestions?
Upvotes: 1
Views: 377
Reputation: 6179
I use this approach.
Declare two activity fields:
private int listItemPressedPos;
private long listItemPressedId;
Declare an onItemLongClick
event and use them to save position and id of listItem clicked
myList.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapter, View v, int pos, long id) {
listItemPressedPos = pos;
listItemPressedId = id;
return false;
}
});
In onCreateContextMenu method get the row from database by Id and change the menu accordingly.
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.dispositivos_list_context, menu);
//Change this part accordingly your needs
if(tbDispositivos.isSelected(listItemPressedId)){//get value from database
//Alter the menuItem
menu.findItem(R.id.dispositivosContextItemDelete).setVisible(false);
}
}
Upvotes: 2