Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to set value of edittext with same id in different inflated linearlayout views?

I have one main layout with one LinearLayout. In this LinearLayout I inflates multiple xml layouts that contain a form with multiple text fields(3 EditText) in it.

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 data of these three forms.

For doing this I am targeting the main layout "LinearLayout" and then counting its child. After counting its child I called child views of Main LinearLayout by its position and then get EditText data and save into a list. Then I moved this list to next page. Everything works fine till then. But if user comes back on previous page, all inflated layouts were gone. So, I count the size of list on resume and set the values what users wrote last time.

The problem is when I set the values of EditText according to its view position. Only last object(of list) value is shown in all inflated layouts. Means when for loop ends it sets last object value in all layouts. This is my method for setting values against a view:

private void addFormDataOnResume(LinearLayout viewForm,Traveller otherTraveller)
{
    EditText dateOfBirthEt = (EditText)viewForm.findViewById(R.id.dateOfBirth);

    dateOfBirthEt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            showDatePickerDialog(dateOfBirthEt);
        }
    });

    dateOfBirthEt.setText(otherTraveller.getOtherDateOfBirth());

    EditText firstNameET = (EditText)viewForm.findViewById(R.id.firstName);
    firstNameET.setText(otherTraveller.getOtherFirstName());

    EditText lastNameEt = (EditText)viewForm.findViewById(R.id.lastName);
    lastNameEt.setText(otherTraveller.getOtherLastName());
}

My loop code:

int otherTraSize = otherTravellersData.size();
//adultsForms is the main linear layout in which I am adding views
for(k=0; k < otherTraSize; k++)
{
addFormOnResume();//Function for adding form layout
int viewPos = adultsForms.getChildCount();
if(viewPos>0)
{  
addFormDataOnResume(adultsForms.getChildAt(viewPos-1), otherTravellersData.get(k));
}
}

My FUnction for adding forms:

private void addFormOnResume()
{
    LinearLayout viewForm = (LinearLayout)layoutInflater.inflate(R.layout.other_traveller_form, null );
            adultsForms.addView(viewForm);

}

I debug my code, all data of list seems fine. Objects are in proper order with proper values.
Please help me why only last object value is set to all of the inflated forms.

Any help would be really appreciated......Thanks in Advance.

like image 656
Waqas Ahmed Khan Avatar asked Dec 03 '25 12:12

Waqas Ahmed Khan


1 Answers

Finally I got the answer of my own question. By default when an activity goes in onPause mode. Android saves the EditText value against an id and when we resume it restore the last saved value of EditText. So, in my case it restoring last form values in all layout forms. The solution of my problem is to set android:saveEnabled="false" on the widget entry on the XML. I added this in all EditText. Android no longer saves my data and onResume I can set value of my form fields whatever I want to, by simply getting childView from specified position even if they have same Id.

like image 64
Waqas Ahmed Khan Avatar answered Dec 05 '25 02:12

Waqas Ahmed Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!