views:

62

answers:

2

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, menuInfo);  
    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.

+1  A: 
  1. It's impossible that v.getId() returns false. It returns an integer, which won't be a boolean ever.
  2. On the other hand, what's this supposed to do?: menu.add(0, v.getId(), 0, v.getId());. You are passing v.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 meant menu.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.

Cristian
I was just using that to see what v.getId() was returning.
Joel Auterson
Thanks, but the v.getId() is still coming out as a boolean.
Joel Auterson
@Joel are you kidding. Read this carefully: v.getId() WON'T return a boolean EVER! Because it returns an integer, not a boolean. It's completely impossible. It's more feasible the God existence than that you are saying. Did you try `equals` method?
Cristian
So totally feasible and even likely?
Joel Auterson
Yes, I'm positive. I've tried the equals method also, but it comes AFTER where I'm checking for what v.getId() equals.
Joel Auterson
+1  A: 

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.

JRL
An int? So it won't be something like R.raw.sound?
Joel Auterson
R.raw.sound is an integer, if you haven't noticed :D
Cristian
...is it?! :L I'm more used to Python, I'm afraid.
Joel Auterson
So why AM I getting false?
Joel Auterson