0

I have one main layout with one LinearLayout. In this LinearLayout I inflates xml layouts that contain a form with multiple edittext fields(4 EditText) in it. every time i click on add button xml layouts that contain a form with multiple edittext fields(4 EditText) add in linearlayout.

On first attempt I show only one form. There is button for adding more forms. Suppose If user clicks on "Add" button two times then user have three total forms. I successfully get all these three forms.

i am set Tag child view so easy to get one bye one edittext value

but my problem is when i click on save button all edittext data get and add in array ..

Button buttonAdd = (Button) findViewById(R.id.pdBtnAddDoc);
        buttonAdd.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

            myRoot = (LinearLayout) findViewById(R.id.linearLayoutAdd);
            LayoutInflater inflater = (LayoutInflater)PrimaryDoctorFragment.this.getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);

            childView = inflater.inflate(R.layout.fragment_primary_doc_add,myRoot);

                // childView.setId(tag++);
                childView.setTag(tag++);

            }
        }); 

here is my save button:-

btnSave.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

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

                    System.out.println("i:- " + i);

                    View aView = myRoot.findViewById(i);
                    EditText name = (EditText) aView
                            .findViewById(R.id.childEtName);
                    Log.d("Gettext :- ", name.getText().toString());

                }

            }
        });

on save button all four text field data get and add in array..

  • Notice that **tag** and **id** are not the same thing. For each child you are setting a tag `.setTag(tag++)`, then you are trying to get the view by id `.findViewById(i)` where **"i"** is a **Tag** not an **ID**. Give a try with `.findViewWithTag(i);` – Rami Mar 18 '15 at 10:59
  • i try .findViewWithTag(i) then also its crashed in this EditText name = (EditText) aView.findViewById(R.id.childEtName); here is stop.. – Priyank Prajapati Mar 18 '15 at 11:12
  • Because it didn't find the view, so it return null to *aView*. Where is the initialization of **tag**? and did you have the same result with `childView.setId(tag++);` and `myRoot.findViewById(i);`? – Rami Mar 18 '15 at 11:27
  • tag initializ = 0 ; at top , yes the same result with childView.setId(tag++); – Priyank Prajapati Mar 18 '15 at 11:47

0 Answers0