Reputation: 9373
Hi I'm trying to set up a check that looks to see if the currently set picture is x and if it is x it does stuff else if its y it does other stuff.
Button sharebtn = (Button)findViewById(R.id.Sharebtn);
sharebtn.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
ImageView picture = (ImageView) findViewById(R.id.BTN);
if (picture.equals(R.drawable.x)){
//do really cool stuff
}else if (picture.equals(R.drawable.y){
//do stuff
}
}
});
with this code the button just freezes and doesn't do anything. It doesn't show focus either only.
EDIT: more info The code is for a favorites icon. So if the user is on the page that is marked as a favorite I want the icon to be a filled star and otherwise an empty star. The problem that I'm having is i want them to be able to click the start to do the code that either adds the page to favs or removes it. I thought the easiest way to do that would be by checking to see what the icon currently is as its set in the oncreate. The icon works properly so it will show filled if a fav and viseversa however the code to get it delete or add to favs (the snippet from above) is not working. I've also tried:
ImageView picture = (ImageView) findViewById(R.id.favsBTN);
java.io.File file = new java.io.File(persistentCarsDir+"/"+vin);
if (file.exists()) {
removefavorite();
picture.setImageResource(R.drawable.nostar);
}else if(!file.exists()){
favorite();
picture.setImageResource(R.drawable.star);
}
This code aboves works ^ i just had the file path wrong :)
Upvotes: 0
Views: 1889
Reputation: 3430
In your code you are comparing an imageview with an integer, that won't work.
To get the drawable of an ImageView you should call getDrawable. And to get the actual drawable for an resource id call getResources().getDrawable(R.drawable.x)...
But afaik there is no way to compare drawables... The easiest way would probably be to use the setTag method for the imageview. So you can set its tag to "X" or "Y" whenever you change its drawable...
Then check the tag and decide what to do...
EDIT: Or you use a checkbox instead of the imageview. You can simply check "isChecked" to find out whether it's been checked or unchecked. You can change the graphic of the checkbox with "setButtonDrawable" to use your star. And if you don't want to manually switch between filled and not filled star use a statefull drawable where you can have the filled star for "checked" and the not filled star for not checked...
hope this helps...
Upvotes: 1
Reputation: 1086
I think View.setTag() is what you want to do. In your ListAdapter you create the View object for the list.
public View getView(int position, View convertView, ViewGroup parent) {
View view = < create view here >;
Object imageLocator = < Enough information to locate the image later >
view.setTag(imageLocator);
return view;
}
Then in onClickListener
public void onClick(View view)
{
Object imageLocator = view.getTag();
if (imageLocator != null) {
// use imageLocator info to find the image
// do your really cool stuff
} else {
// just do other stuff
}
}
Upvotes: 1
Reputation: 2049
R.drawable.x and R.drawable.y are integers and you are comparing them with an ImageView which will always return false.
You would have to convert your drawable and ImageView to the same object and then compare them.
Upvotes: 0
Reputation: 722
This is not the good way to do so, because you're trying to compare an ImageView to a int (R.drawable.x is actually a int). So those two objects can never be equal. You can not either compare an ImageView to a drawable, because there are not the same object. Maybe you can give a bit more precision of the context of use, so that we can try to provide a solution for your problem ?
Upvotes: 0