1

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

ashokk
  • 437
  • 1
  • 5
  • 13
  • 1
    Check this out: http://stackoverflow.com/questions/7200108/android-gettext-from-edittext-field Or you can read EditText's [documentation](http://developer.android.com/reference/android/widget/EditText.html). – Rainulf Apr 28 '12 at 07:50

3 Answers3

3

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();
 }
5hssba
  • 8,079
  • 2
  • 33
  • 35
2

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);
}
Arif Nadeem
  • 8,524
  • 7
  • 47
  • 78
1

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;
        //...
    }
}
Conscious
  • 1,603
  • 3
  • 18
  • 16