How to get data from each dynamically created EditText in Android?

53,185

Solution 1

In every iteration you are rewriting the ed variable, so when loop is finished ed only points to the last EditText instance you created.

You should store all references to all EditTexts:

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

for (int i = 0; i < count; i++) {   

    ed = new EditText(Activity2.this);
    allEds.add(ed);
    ed.setBackgroundResource(R.color.blackOpacity);
    ed.setId(id);   
    ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    linear.addView(ed);
}

Now allEds list hold references to all EditTexts, so you can iterate it and get all the data.

Update:

As per request:

String[] strings = new String[](allEds.size());

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

Solution 2

You can also do like this by taking an Array of EditText. You should store all references to all EditTexts:

EditText ed[] = new EditText[count];    
for (int i = 0; i < count; i++) {   

    ed[i] = new EditText(Activity2.this);

    ed[i].setBackgroundResource(R.color.blackOpacity);
    ed[i].setId(id);   
    ed[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    linear.addView(ed[i]);
}

and you can use for loop to get the value from the EditText .

 for(int i = 0; i < ed.length; i++){

       Log.d("Value ","Val " + ed[i].getText());
  }

Solution 3

            String[] strings = new String[allEds.size()];
            String[] strings1 = new String[allEdn.size()];

            for(int i=0; i < allEds.size(); i++){
                strings[i] = allEds.get(i).getText().toString();
                strings1[i] = allEdn.get(i).getText().toString();
                System.out.println("data in edittext name is  " +strings[i]);
                System.out.println("data in edittext number is " +strings1[i]);
            }

for multiple edittext

Solution 4

I have a for loop generating dynamic TextViews within TableRows from a JSON string. Here's what I did:

// Generate Dynamic tablerows and set a unique id
//m_jArry is a JSON Array

for(int i = 0; i < m_jArry.length(); i++)
{
    jo_inside = m_jArry.getJSONObject(i);
    TableRow row = new TableRow(getApplicationContext())
    row.setId(jo_inside.getInt("id"));
    ...        
    findViewById(row.getId())
    ...
}

I am using findViewById(row.getId()) to set the dynamic TextView text data to EditText fields in my app. For those of you who do not understand JSON, there are several good tutorials out there (I'm sure there are great explanations here on Stack Overflow) for the Android platform. I'm still an Android newbie, but I hope this is helpful to some of you out there.

Share:
53,185
Mohammad Usman
Author by

Mohammad Usman

Updated on December 07, 2020

Comments

  • Mohammad Usman
    Mohammad Usman over 3 years

    I have successfully created EditTexts depending on the user input in Android, and also I have assigned them unique ID's using setId() method.

    Now what I want to do is to get values from the dynamically created EditTexts when the user tap a button, then store all of them in String variables. i.e. value from EditText having id '1' should be saved in str1 of type String, and so on depending on the number of EditTexts.

    I am using getid(), and gettext().toString() methods but it seems a bit tricky... I cannot assign each value of EditText to a String variable. When I try to do that a NullPointerException occurs, and if it is not the case where no user input data is shown, I display it in a toast.

    Heres, the code:

    EditText ed;
    
    for (int i = 0; i < count; i++) {   
    
            ed = new EditText(Activity2.this);
            ed.setBackgroundResource(R.color.blackOpacity);
            ed.setId(id);   
            ed.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT));
            linear.addView(ed);
    
    }
    

    How do I now pass the value from each EditText to each different string variable? If some body could help with a sample code it would be nice.

  • Mohammad Usman
    Mohammad Usman about 13 years
    Can you please also help me on retrieve and save values from allEds to string array too .. tat would be so nice (:
  • Mohammad Usman
    Mohammad Usman about 13 years
    Well shailendra, it was about the correct reference of edittexts .. i was getting value from only last referenced edittext your code might come handy some other day hehe Thanks for help :)
  • Mohammad Usman
    Mohammad Usman about 13 years
    Thanks much appreciate, but i would like you to consider the below statement again as im getting an assignment error in this: Left hand side must be a variable? String[] strings = new String[](allEds.size());
  • Peter Knego
    Peter Knego about 13 years
    My bad. Should be ` String[] strings = new String[allEds.size()];`
  • Nikhil Agrawal
    Nikhil Agrawal about 11 years
    @Peter Knego your answer has been copied here stackoverflow.com/questions/16102773/… By gowtham I have complained to moderator please tahe necessary steps.
  • Sarbjyot
    Sarbjyot about 7 years
    Hey can you please clear that what we used as id here, I am little bit confused. I am getting empty values from edittext.
  • hetsgandhi
    hetsgandhi over 5 years
    Thanks a lot for the solution. It works perfect for my problem!