1

This is my code..I have created a dynamic screen where i am generating one textview and edittext under a for loop.but after button click it is only getting the last input.I need to get all the inputs of editText and have to pass them to new screen..guyss..plzz help me out.here is my code..

            runOnUiThread(new Runnable() {
            public void run() {
                final LinearLayout findViewById = (LinearLayout) findViewById(R.id.dynamicInputs);
                // TextView textView = (TextView) findViewById(R.id.name);
                TextView textView = new TextView(Activity_UserInput.this);
                textView.setText(map.get(KEY_NAME) + " :");
                textView.setTextColor(Color.BLACK);
                textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

                findViewById.addView(textView);

                final EditText editText = new EditText(
                        Activity_UserInput.this);

                editText.setText("");

                editText.setFocusableInTouchMode(true);
                editText.requestFocus();

                findViewById.addView(editText);
                final ArrayList<EditText> allEds = new ArrayList<EditText>();
                allEds.add(editText);


                btn.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {

                        // it = new Intent(Activity_UserInput.this,
                        // Submition_Activity.class);
                        // it.putExtra("input", arryList);
                        // System.out.println("get the input"
                        // + arryList);

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

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

                        }

                    }
                });

            }
        });
Parinita
  • 87
  • 2
  • 13
  • hi...Can you pls tell me for what you have used this final ArrayList allEds = new ArrayList(); allEds.add(editText); – Srinivasan Aug 08 '13 at 10:45
  • To get text from EditText we used like - String s = ediText.getText() To send it to next screen add that string using putExtra method in Intent – Srinivasan Aug 08 '13 at 10:46
  • I wanted to add edittext in an arraylist – Parinita Aug 08 '13 at 10:47
  • But that only gives me the last input of an edittext.As I have five edittext in my screen but i am getting only the last input. – Parinita Aug 08 '13 at 10:50

4 Answers4

1

Here, in your code arraylist of EditText created every time.So initialize allEds at startup

final ArrayList<EditText> allEds = new ArrayList<EditText>();

and then add EditText to arraylist

 final EditText editText = new EditText( Activity_UserInput.this);
             editText.setText("");
     editText.setFocusableInTouchMode(true);
      editText.requestFocus();
    findViewById.addView(editText);
   allEds.add(editText);
Asha Soman
  • 1,846
  • 1
  • 18
  • 28
0

You are creating a new allEds everytime you put final ArrayList<EditText> allEds = new ArrayList<EditText>(); So the allEds will contain just one element.

Try creating it outside the for loop.

Renan Bandeira
  • 3,238
  • 17
  • 27
0

You have created only one edittext and also you have added only one edittext to the allEds arraylist. You have also declared the size of the string[] to be the size of the allEds arraylist. So, the length of string[] will be 1 only and the for loop will execute only once. so that, you will get only one input.

If you want to create multiple edittext create them inside a for loop. Or if you are already creating them under a for loop try to declare the following line outside the loop instead of declaring inside.

{ArrayList<EditText> allEds = new ArrayList<EditText>();}
Hariharan
  • 24,741
  • 6
  • 50
  • 54
0

My Layout :

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/mTopLayout" ></LinearLayout>

Within my activity

LinearLayout mLayout = (LinearLayout) findViewById(R.id.mTopLayout);

 ArrayList<EditText> mCheck = new ArrayList<EditText>();


    EditText mEdit = new EditText(MainActivity.this);
    mEdit.setText("");
    mEdit.setFocusableInTouchMode(true);
    mEdit.requestFocus();
    mLayout.addView(mEdit);
    mCheck.add(mEdit);

    EditText mEdit1 = new EditText(MainActivity.this);
    mEdit1.setText("");
    mEdit1.setFocusableInTouchMode(true);
    mEdit1.requestFocus();
    mLayout.addView(mEdit1);
    mCheck.add(mEdit1);

    EditText mEdit2 = new EditText(MainActivity.this);
    mEdit2.setText("");
    mEdit2.setFocusableInTouchMode(true);
    mEdit2.requestFocus();
    mLayout.addView(mEdit2);
    mCheck.add(mEdit2); ......

Within onClick method

String[] st = new String[mCheck.size()];

for(int i=0;i<st.length;i++){
    st[i] = mCheck.get(i).getText().toString(); 
}
Log.i("Recived String", st.length+"");
for(int j=0;j<st.length;j++){
    Log.i(" String", st[j]+""+j);  }

It works fine for me....Please review your layout and let me know.... thanks...

Srinivasan
  • 4,481
  • 3
  • 28
  • 36