ashokk
ashokk

Reputation: 447

Creation of EditText Dynamically and get their text from each of EditText

I have created EditText views Dynamically now I want to get their texts into one list or array, How can I do that?

Upvotes: 1

Views: 3287

Answers (3)

Conscious
Conscious

Reputation: 1663

if your problem is accessibility to these views:

// assume ll is your container LinearLayout and your EditTexts inserted in it
LinearLayout ll;
int nViews = ll.getChildCount();

for (int i = 0; i < nViews; i++) {
    View child = ll.getChildAt(i);
    if (child instanceof EditText ) {
        EditText edt= (EditText) child;
        //...
    }
}

Upvotes: 1

Arif Nadeem
Arif Nadeem

Reputation: 8604

You can create two List's one for storing dynamically created EditText's and one for storing their text's.

Then iterate through the EditText list using a for-each loop and get the text of each EditText and add it to List of text's.

List<EditText> myList = new ArrayList<EditText>();
List<String> etText = new ArrayList<String>();
EditText myEt1 = new EditText(this);
EditText myEt2 = new EditText(this);
EditText myEt3 = new EditText(this);
//so on...

myList.add(myEt1);
myList.add(myEt2);
myList.add(myEt3);

for(EditText et : myList){
String settext = et.getText().toString();
etText.add(settext);
}

Upvotes: 2

5hssba
5hssba

Reputation: 8079

You have to get the reference for your edittexts while you are creating them dynalically like this..

    EditText ed;
    List<EditText> allEds = new ArrayList<EditText>();

 for (int i = 0; i < count; i++) {   
   //count is number of edittext fields
ed = new EditText(MyActivity.this);
allEds.add(ed);

linear.addView(ed);
}

now allEds will have the references to your edittexts.. and you can get the text like this..

 String [] items=new String[allEds.size()]
 for(int i=0; i < allEds.size(); i++){
 items[i]=allEds.get(i).getText().toString();
 }

Upvotes: 3

Related Questions