-2

I have a scroll view and a linear layout. Depending upon the data i am getting i generate a view respectively and inject it to the main layout.

Now each view contains certain value but no id as they are injected dynamically. How to access the value from each for further processing. I have tried on click listener but did not get good results. I want to add one thing these views can vary some are multiple. some are single etc. I need your valuable suggestion and will post code if necessary.

Shubham AgaRwal
  • 4,355
  • 8
  • 41
  • 62

3 Answers3

2

As already mentioned in the comments: View has a setId(int id) method.
Use that to access your view. Store them in some list, map or write to file system for further usage. You could also use SharedPreferences to pass the information of the views to another intent.

Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
  • same doubt about conflicting id issue. Is it correct to give id like 1 2 3. will it not conflict. What if i use layout id and some no appended togather as viewId . or what if i use id and tag. What approach should i use – Shubham AgaRwal Jun 26 '15 at 07:26
  • It depends on what are you planning to do. Please describe you general task. – Konstantin Kiriushyn Jun 26 '15 at 07:29
1

First of all, the View Class has a setId() method. As all your views inherit from this class, they also have this method. As an id you could specify the views in an xml file before, so you won't have any conflicts with double or more used ids, as descibed here.

Community
  • 1
  • 1
Sebastian Walla
  • 1,104
  • 1
  • 9
  • 23
1

If you are using an API >= 17 you can use the generateViewId() method if you are worried about id collision.

You may also consider using setTag() instead of setId() as well.

user4989692
  • 1,609
  • 1
  • 20
  • 25