0

I want to bind some information by attaching tags to a view. So when the view is clicked I can retrieve this information with its tag. However, I meet problem when using an int array as the tag. I save two integers by following samples:

int arr=[x,y];
view.setTag(arr);
parentView.addView(view);//parentView is a list/LinearLayout of such views

Later, when I want to find the view using

int tmp=[i, j];//i=x, j=y in values
View target=parentView.findViewWithTag(tmp);

The returned target is null. Why does it not return the correct view?

Edit:

I'm not sure if it's because of object reference and comparison issue. I tried to combine integers x and y into a string like "x,y", and search the view like parentView.findViewWithTag("x,y") and it can find the view.

mrmoment
  • 727
  • 2
  • 10
  • 34
  • if you are dealing with arrays - the comparison is between their references not their values (new object with same values will return false because it differ in reference from other object). how about creating a custom view with custom attributes? http://developer.android.com/training/custom-views/create-view.html – ymz Nov 30 '14 at 12:41
  • Moreover, I cannot find `findViewByTag()` on `View` or `ViewGroup`. – CommonsWare Nov 30 '14 at 12:41
  • @CommonsWare That's findView*WITH*Tag() – mrmoment Nov 30 '14 at 12:51
  • @ymz I also doubt it's because the object reference issue. But Android view's tag can be an Object (e.g., a customized class), right? Then how to realize findViewWithTag(object) in this case? – mrmoment Nov 30 '14 at 12:53
  • Ah, I see. I haven't used that in ~5 years, and had forgotten they switched "by" with "with". However, [since `int[]` is not going to be autoboxed](http://stackoverflow.com/a/1770906/115145), I have no idea what your tag is winding up as. I would recommend that you use actual objects, not arrays of primitives, as your tag. "it can find the view" -- and hence this demonstrates the point. – CommonsWare Nov 30 '14 at 12:55
  • @CommonsWare OK, that could be a reason. Will check it. – mrmoment Nov 30 '14 at 12:56

1 Answers1

0

Although didn't do verification experiments, a guess is mentioned by @CommonsWare. And real objects should solve the problem.

mrmoment
  • 727
  • 2
  • 10
  • 34