Skip to content Skip to sidebar Skip to footer

Android Creating Array Of Widgets In The Xml And Reading In Activity

In my project i have several TextBoxes which i have to fill dynmically. I have the values to be filled in a list, and i want to apply setText to the text boxes at the time of itera

Solution 1:

You would try using reflection:

for(int i = 0; condition;i++){
    try {
        int id = (Integer) R.id.class.getField("textbox"+i).get(null);
        TextView textView = (TextView) findViewById(id);
        textView.setText("i th text from your list");
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SecurityException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Solution 2:

well you can make your own array of textboxs...

    ArrayList<TextBox> textbox = newnew ArrayList<TextBox>();
    textbox.add((TextBox)findViewById(R.id.textbox1);  
    textbox.add((TextBox)findViewById(R.id.textbox2); 

then use it like: textbox[1]

Post a Comment for "Android Creating Array Of Widgets In The Xml And Reading In Activity"