Stephen
Stephen

Reputation: 10079

Store dynamic edit text value in database

I have created Dynamic Edit text programatically.Inside the custom dialog window,you can see the edit text code.

What I need:

For Eg: If I enter the edit text value as 3,then three edit text will be created.My only problem is,I have to save the three edit text value in the database which I was created in customSave button.

Detail.java:

EditText value;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

setContentView(R.layout.detail);

value = (EditText) findViewById(R.id.value);

btnAddCount.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    final Dialog dialog = new Dialog(Detail.this);

                    //setting custom layout to dialog
                    dialog.setContentView(R.layout.custom_dialog_layout);

                    dialog.setTitle("Add List");

                    //adding button click event
                    final Button createEditText = (Button) dialog.findViewById(R.id.button);

                    createEditText.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            count = value.getText().toString();


                            int i = Integer.parseInt(count);


                            if (i >= 0) {
                                do {


                                    et = new EditText(VehicleDetail.this);


                                    FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                                    params.gravity = Gravity.CENTER;

                                    ((LinearLayout) dialog.findViewById(R.id.container)).addView(et);

                                    //           db.execSQL("insert into PassengerDetail (PASSENGER_ID) values('" + et.getText().toString() + "')");


                                    i--;
                                    createEditText.setVisibility(View.INVISIBLE);


                                } while (i > 0);

                                Toast.makeText(getApplicationContext(), "Running", Toast.LENGTH_LONG).show();

                            }
                        }
                    });


                    customSave.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {


                            db.execSQL("insert into Detail(NUMBER_ID) values('" + et.getText().toString() + "')");


                            Log.d("Edit", "editText" + et);

                            dialog.dismiss();
                        }
                    });

                    dialog.show();
                }


            });

If I enter the edit text value as 3,while click the customSave button,I have to store the three edit text value in 3 rows in database.But now only one edit text would be storing in the database.

Anyone can help me with this.Thank You.

Upvotes: 0

Views: 454

Answers (1)

Satty
Satty

Reputation: 1372

Declare ArrayList of Edittexts global as :

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

In your do while loop add all newly created edit text objects :

editTexts.add(et);

Then in customSave onclickListner get all edit texts and save in database as :

for (Iterator iterator = editTexts.iterator(); iterator.hasNext();) {
     EditText editText = (EditText) iterator.next();
     db.execSQL("insert into Detail(NUMBER_ID) values('" + editText.getText().toString() + "')");
      Log.d("Edit", "editText" + et);
}
editTexts.clear();

Upvotes: 3

Related Questions