I have a context menu. The function v.getId() is supposed to return the id, but does not - instead it returns 'false'.
Here is my code:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, men开发者_开发知识库uInfo);
menu.setHeaderTitle("Options");
menu.add(0, v.getId(), 0, v.getId());
menu.add(0, v.getId(), 0, "Save Notification");
}
@Override
public boolean onContextItemSelected(MenuItem item) {
if(item.getTitle()=="Save Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Save Notification"){function2(item.getItemId());}
else {return false;}
return true;
}
public void function1(int id){
//Stuff
}
public void function2(int id){
if (id == R.raw.tedcake){
Toast.makeText(this, id, Toast.LENGTH_SHORT).show();
}
The buttons were registered for a context menu elsewhere in the code.
Why is it returning false?
Thanks.
I had the same issue...in the folder res/values you should have a file ids, for each item there are four attributes (name, type, format, value). In my case the field "value" was "false"
view.getId()
returns an int, not false. If the view does not actually have an ID, it returns NO_ID, which is a constant equal to -1. One issue I see with your code is that you are comparing strings using ==. You should use equals instead. There's plenty of related SO posts about that, if you want to know more.
- It's impossible that
v.getId()
returnsfalse
. It returns an integer, which won't be a boolean ever. - On the other hand, what's this supposed to do?:
menu.add(0, v.getId(), 0, v.getId());
. You are passingv.getId()
in the last parameter, which will cause problems (since it's waiting for an String resource identifier, not a view resource identifier). Maybe you meantmenu.add(0, v.getId(), 0, "Save Ringtone");
?
With regards to your question: "why am I getting false?". It's because of this:
if(item.getTitle()=="Save Ringtone"){function1(item.getItemId());}
else if(item.getTitle()=="Save Notification"){function2(item.getItemId());}
else return false;
It's a common problem for new Java developers. The problem is here: item.getTitle()=="Save Ringtone"
; specifically the problem is ==
. That's not the way you compare objects in Java. You have to use the equals
method, this way:
if(item.getTitle().equals("Save Ringtone")){function1(item.getItemId());}
else if(item.getTitle().equals("Save Notification")){function2(item.getItemId());}
else return false;
If you use the ==
operator with objects, you are comparing the references to the object, not the object.
it's because most functions in android take string not int, so does toast. try String.valueOf(view.getId()) to toast u will see the id. ur welcome
I had a similar issue to you. view.getID()
will always return an integer, so I decided to see what would happen if I cast the integer to a string in the toast. This method will show you the ID. I believe the reason you see false is because the toast errors out when you supply an integer instead of a char sequence.
精彩评论