0

I'm adding TextViews programmatic using a for-loop and use setId(Int int) to set unique id for that perticular Textview. But now I want to search textView on the basis of that id. How can I search?

erorr occurd 'app stopped unexpectedly...' Here is my code.

public class Idtest extends Activity {
    TextView tv;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        LinearLayout ll=new LinearLayout(this);
        tv=new TextView(this);

        tv.setText("Hello");
        ll.addView(tv);

        tv=new TextView(this);
        tv=(TextView)ll.getChildAt(1);

        setContentView(tv);


    }
}
Felix
  • 88,392
  • 43
  • 149
  • 167
Nirav
  • 5,700
  • 4
  • 34
  • 44

3 Answers3

2

tv is obviously null at the end (the LinearLayout only has one child, which is at index 0), so you're basically calling setContentView(null) which results in an exception. It's not clear for me what you're trying to do (your code is pretty messed up).

Supposing you are trying to show multiple TextViews in a LinearLayout, here's my suggestion:

public class Idtest extends Activity {
    LinearLayout mainLayout;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mainLayout = new LinearLayout(this);
        setContentView(mainLayout);

        for (int i=0; i < 10; i++) {
            TextView tv = new TextView(this);
            tv.setText("Hello " + i);
            mainLayout.addView(tv);
        }
    }
}

If, at any later point, you need one of the TextViews, do:

TextView tvX = mainLayout.getChildAt(X); // where X is between 0 and 9

Also, please note that creating layout from code is evil. If you can avoid it, please do. For example, if the number of TextViews is dynamic, then it's perfectly normal to create them from code (although you could inflate them). However, it's not advisable to also create the LinearLayout from code. You should have that in an XML. If it's possible for you to also have the TextViews in an XML, that would be even better.

Felix
  • 88,392
  • 43
  • 149
  • 167
  • Thanks, for code. But though I couldn't fetch perticular text box. Force to close app occured. TextView tvX = (TextView) mainLayout.getChildAt(2); setContentView(tvX); These Two line I have written. – Nirav Mar 08 '11 at 09:55
  • Why do you `setContentView(tvX)`? It makes no sense, please explain what you are trying to do better. – Felix Mar 08 '11 at 10:40
  • I am just seeing that TextView on screen which I have fetched. To confirm that Selected TextView is correnct. – Nirav Mar 08 '11 at 11:13
  • I don't mean to offend you, but please work on your English, it's really hard to understand what you want. If you just want to check that the correct `TextView` is selected you can use some other visual cue, such as `tvX.setText("I am selected!");`. – Felix Mar 08 '11 at 20:02
  • Yes, You are right. I should improve my English I will. Thanks for grate advice And thanks for you answer. It's working. – Nirav Mar 09 '11 at 04:22
1

There is another option too...we have setTag() and getTag() methods. While adding a textview give a tag for it and whenever u want to do a search use getTag().

Anju
  • 9,379
  • 14
  • 55
  • 94
1

I suppose you are adding your TextView controls to some ViewGroup (e.g. LInearLayout)?

You can iterate through the ViewGroup child views using getChildCount() / getChildAt(index) and compare the child view id with the one you're searching for.

basv
  • 455
  • 4
  • 11