3

I'm trying to generate random ids for views as shown in following screenshot. But it didn't work. It got null. How should I findViewById ?

enter image description here

Zin Win Htet
  • 2,142
  • 4
  • 26
  • 44
  • You're confused. `setID` and `findViewByID` both are different. `findViewByID` allows you to refer to the particular view with the id you give in the xml file. – Aniruddha Aug 05 '14 at 11:53
  • Why are you trying to do this. This is a wrong thing to do. I mean when you create a view you give it an id then how can you expect to access that view by generating some random id. – Sar009 Aug 05 '14 at 11:56
  • So.., what am I supposed to do to get back textView ? – Zin Win Htet Aug 05 '14 at 11:56
  • @Sar009 , cuz I've to create a lot views dynamically and got to reuse them from somewhere in the project. – Zin Win Htet Aug 05 '14 at 11:58
  • 2
    why not try ListView when you have to create a LOT OF views dynamically. – Sar009 Aug 05 '14 at 12:02
  • I can't use ListView. Cuz my views depends on JSON from server. I don't predict what will be coming. – Zin Win Htet Aug 05 '14 at 12:03

5 Answers5

13

use textView.setId(View.generateViewId()) introduced in API 17.

Montassir Ld
  • 456
  • 4
  • 12
2
 TextView tv = new TextView(this);

This means you're creating the TextView dynamically. So you don't need to do findViewById. findViewById is used when the view with id is present in xml file.

Remove the TextView cloneTextView = (TextView) findViewById(randomNo) line. Your question is vague, I tried to explain.

Aniruddha
  • 4,421
  • 1
  • 19
  • 38
  • Ok. Let's say I made TextView tv = new TextView(this). How can I call it from another activity without knowing id? Don't say to make static. My views will be create dynamically so there's no static. – Zin Win Htet Aug 05 '14 at 12:00
  • You want the value of that `TextView`? If so, you can pass the value using Intent. – Aniruddha Aug 05 '14 at 12:02
0

I got my own solution... It should be like that..

Random r = new Random();
    randomNo = r.nextInt(1000+1);

    TextView textView = new TextView(this);
    textView.setId(randomNo);
    linearLayout.addView(textView);
    int childCount = linearLayout.getChildCount();
    for(int i=0;i<childCount;i++){
        if(linearLayout.getChildAt(i).getId()==randomNo){
            TextView cloneTextView = (TextView) linearLayout.getChildAt(i);
            cloneTextView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
            cloneTextView.setText("I'm a clone...!");
            linearLayout.removeAllViews();
            linearLayout.addView(cloneTextView);
        }
    }

It works and that's what I want. Thank you all.

Zin Win Htet
  • 2,142
  • 4
  • 26
  • 44
0

Something like this may work.

But I'm not sure about possible performance and memory issues, since it will return an instance of a view (if found). During a little test sequence it never hit an existing id, with other words the first random number was always ok.

private int createUniqueId() {

    int id = RandomUtils.nextInt();

    while(findViewById(id) != null) {
        //id is not unique, try another one...
        id = RandomUtils.nextInt();
    }
    //return unique id
    return id;
}
Dirk
  • 1,871
  • 1
  • 19
  • 25
0

You can create UUID (universal unique identifier) as follow :

String id= UUID.randomUUID().toString();
Osama Ibrahim
  • 889
  • 10
  • 13