Comparing a view's tag to an integer
17,228
Solution 1
I think your Tag is rather a String than an Integer.
If that's the case convert your Integer toString() and check if it equals().
Solution 2
Tag is an Object, so put an Integer
:
/*
* UseValueOf
* ----------
* Priority: 4 / 10
* Severity: Warning
* Category: Performance
*
* You should not call the constructor for wrapper classes directly, such as`new
* Integer(42)`. Instead, call the valueOf factory method, such as
* Integer.valueOf(42). This will typically use less memory because common
* integers such as 0 and 1 will share a single instance.
*/
//MyView.setTag(new Integer(42));
MyView.setTag(Integer.valueOf(42));
Then retrieve the value like this:
int tagValue = (Integer)MyView.getTag();
Solution 3
You have to convert buttons[k].getTag() in integer.
Do this:
if(grid[i][j] == Integer.parseInt(buttons[k].getTag().toString())){
Solution 4
You pass in an integer and get in an index variable. Here is the code snippet
int Index = Integer.parseInt(v.getTag().toString());
System.out.println(Index);
Author by
Benny292
Updated on June 16, 2022Comments
-
Benny292 almost 2 years
I'm trying to compare an array of integers to the tags of imageviews I have uniquely made.
using this line:
if(grid[i][j] == buttons[k].getTag()){
I know im on the right tracks, but I can't figure out if i need to cast it or use a method. I know its a simple question, but any help would be greatly appreciated, thanks.