get inputs in an edittext dynamically and pass that to next screen

1.6k Views Asked by At

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();

                        }

                    }
                });

            }
        });
4

There are 4 best solutions below

0
On BEST ANSWER

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);
0
On

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.

0
On

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>();}
0
On

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...