Android : How do I set the DatePicker value in an EditText which I created multiple in a loop

677 Views Asked by At

I have created multiple number of EditText dynamically by using loop. So I have created multiple EditText with the same name. I'am calling the DatePickerDialog when i click an EditText. I can choose the date and am setting done. Once it is completed the chosen date is only setting to the last EditText which I have created.

I want to set the date for all the EditText. Is it possible to set the value in an EditText which is created with the same name?

This is my code.

for(int i =0;i<5;i++)
{
estimate_closedate = new EditText(getActivity());
        estimate_closedate.setLayoutParams(params1);
        estimate_closedate.setWidth(1000);
        estimate_closedate.setHint("Estimated Close Date");
estimate_closedate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                DatePickerDialog dpd = new DatePickerDialog(context,
                        mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }
        });
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        myear = year;
        mmonth = monthOfYear + 1;
        mday = dayOfMonth;
        if (mmonth < 10) {
            month = 0 + Integer.toString(mmonth);
        } else {
            month = Integer.toString(mmonth);
        }
        if (mday < 10) {
            day = 0 + Integer.toString(mday);
        } else {
            day = Integer.toString(mday);
        }
        nextdate = (new StringBuilder().append(myear).append("-")
                .append(month).append("-").append(day)).toString();
        estimate_closedate.setText(nextdate);           
        }
    }
};
1

There are 1 best solutions below

2
On BEST ANSWER

Try this (see my comments) :

for(int i =0;i<5;i++)
{
        EditText estimate_closedate = new EditText(getActivity()); //create new instance
        estimate_closedate.setId(i); //set the id (0-4)
        estimate_closedate.setLayoutParams(params1);
        estimate_closedate.setWidth(1000);
        estimate_closedate.setHint("Estimated Close Date");
        estimate_closedate.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                DatePickerDialog dpd = new DatePickerDialog(context,
                        mDateSetListener, myear, mmonth, mday);
                dpd.show();
            }
        });
}

private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        myear = year;
        mmonth = monthOfYear + 1;
        mday = dayOfMonth;
        if (mmonth < 10) {
            month = 0 + Integer.toString(mmonth);
        } else {
            month = Integer.toString(mmonth);
        }
        if (mday < 10) {
            day = 0 + Integer.toString(mday);
        } else {
            day = Integer.toString(mday);
        }
        nextdate = (new StringBuilder().append(myear).append("-")
                .append(month).append("-").append(day)).toString();
        //estimate_closedate.setText(nextdate);
        for(int i = 0; i < 5; i++)
        {
           EditText estimate_closedate = (EditText) findViewbyId(i); //get the id previously set
           estimate_closedate.setText(nextdate);
        }           
        }
    }
};

The point is in setId when you create the EditText and findViewbyId to find the created EditText, using for loop.