android-viewandroiddynamic-ui

android dynamic generate edittext and use its values


My app. has a button which if it's presses a 2 editText fields apear , and if it's pressed again 2 other editText fields apear and so on. I want to save all the data in this fields in arrayList (or any structure) This is a sample of my code 1- The EditText field that apears:

<EditText  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
    android:minHeight="50dp"

And This is the mainAcvtivity code :

LinearLayout lLayout;
ArrayList<String> mArray = new ArrayList<String>();
int numberOfBoxes = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final LayoutInflater  inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    lLayout = (LinearLayout)findViewById(R.id.layout1);

    final EditText AddEduDate = (EditText)inflater.inflate(R.layout.text, null);
    final EditText AddEduInfo = (EditText)inflater.inflate(R.layout.text, null);

    Button b2 = (Button)findViewById(R.id.button2);

    Button b = (Button)findViewById(R.id.button1);
    b.setOnClickListener(new View.OnClickListener() {


        @Override
        public void onClick(View v) {
            if (lLayout.getChildAt(numberOfBoxes) == null) {

                lLayout.addView(AddEduDate);
                lLayout.addView(AddEduInfo);

                numberOfBoxes += 2; 
            }
        }
    });

Solution

  • If I understand your problem properly, simple

    ArrayList<String> list=new ArrayList<String>();
    list.add(AddEduDate.getText().toString());
    

    should work.