How to find TextView with ID when creating ID's with View.GenerateViewId

How to find TextView with ID when creating ID's with View.GenerateViewId



I'm creating X amount of TextViews dynamically and giving them ID's with View.generateViewId().


TextView textView = new TextView(this);
textView.setText("_");
...
...
int id = View.generateViewId();
textView.setId(id);



And I get some IDs generated e.g 1-5, depending on the number of TextViews. But after searching different forums and the official android site I can't figure out how to access these ID's.



How do I target lets say the TextView with ID = 1 if I want to setText() in that one? Tried some different things like using findViewById and R-class but doesn't seem to work.



Appreciate any direct help or links that are relevant, thanks.






we should keep the ids in some map (name->GENERATED ID) and use the map when needed please

– Hovanes Mosoyan
Sep 16 '18 at 13:08






Agree with Hovanes's suggestion, but consider using Recyclerview if you are generating an indeterminate amount of views dynamically

– Shmuel
Sep 16 '18 at 13:24






@HovanesMosoyan thanks I've tried that. But I still can't figure out how to use it. I can't do "findViewById(1)". How do I write code that says I want the TextView with the generated ID of 1?

– Pa_clae
Sep 16 '18 at 13:24







@Shmuel thanks I've tried that. But I still can't figure out how to use it. I can't do "findViewById(1)". How do I write code that says I want the TextView with the generated ID of 1?

– Pa_clae
Sep 16 '18 at 13:25






You can't use id of 1, what is it you are trying to accomplish here? More context would be helpful

– Shmuel
Sep 16 '18 at 13:41




2 Answers
2



declare the map:


public static final Map<String, Integer> ITEM_MAP = new HashMap<String, Integer>();



keep a track of items:


int id = View.generateViewId();
textView.setId(id);
ITEM_MAP.put("key1", id);

int id2 = View.generateViewId();
textView.setId(id2);
ITEM_MAP.put("key2", id2);



3.and then when needed:


int id = ITEM_MAP.get("key?X");
TextView textView = findViewById(id);



Good luck )






Thanks you very much, now I understand how I could solve it.

– Pa_clae
Sep 16 '18 at 13:53



If you are going for a map, I'd suggest the same as @Hovanes Mosoyan's Answer but, rather than using strings as keys, use the id's as keys and then the value would be the textviews.


private Map<Int, TextView> textViewMap = HashMap();
...
// Add a textview to the map:
textViewMap.put(id, textView);
...
// Retrieve a textview from the map:
textViewMap.get(id);



But, this may have let downs since view id's are not required to be unique and you might get the wrong view. Also, if the id is repeated, it will be overwritten using the map, hence the last view with the same id will be taken.




So Here is a better way to do it



Use tags. View's have their own tags which are strings. Now, as default, a view's tag is empty. This means, we can manipulate it the way we want, and for you, to use it as a unique identifier. Now, the problem is how do we generate a unique id? That's easy, use the current time, as time will never repeat itself.


...
int id = System.currentTimeMillis(); // Use intelli-sense, this might not be the right name of the function
textView.setTag("" + id);
...
// Retrieve the view using the tag
TextView textView = findViewWithTag("" + id);
...



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế