How do I customize DatePickerDialog for getting Tag?

815 Views Asked by At

I have one or more number of EditText with the same name which is created dynamically. I have created these EditText based on the size of Requirement table which i got from the server. Now I'am calling the DatePickerDialog when i clicks the EditText. So Here I want to get the tag and based on the tag I want to set the date which I'am choosing.

Here I can't use the setId. Because already I have used that for the CheckBox.

So If I do again then I will get a conflict. So I want to go with setTag. The tag will be set for every EditText and when I click the onClick of EditText then I need to get the tag. I also need to get the tag in onDateSet function. So that I can get the position of EditText. Could anyone tell me the way how could i customize the DatePickerDialog?

for(int i =0;i<require.size();i++)
{
 requirement = require.get(i);
 date1 = new EditText(getActivity());
 date1.setLayoutParams(params1);
 date1.setWidth(1000);
 date1.setHint("Date");
 String datetag=new String();
        datetag="rid" +   requirement.r_id + "datetag";
        date1.setTag(datetag);
 date.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();
    date.setText(nextdate);           
    }
}
};
2

There are 2 best solutions below

0
On BEST ANSWER

If i got you correctly the date picker is not setting the date for the appropriate EditText.

If that was the problem, you have to do the following changes.

dateEditTextAray[i].setOnClickListener(new OnClickListener() {

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

Instead of passing the context you have to pass v.getContext(). This will solve your problem i guess.

0
On

I think you need to create an array of edit texts and a variable counter for the position clicked in the array like this

int positionClicked;
EditText[] dateEditTextAray = new EditText[require.size()];
for(int i =0;i<require.size();i++)
{
final int j = i;
 requirement = require.get(i);
 dateEditTextAray[i] = new EditText(getActivity());
 dateEditTextAray[i].setLayoutParams(params1);
 dateEditTextAray[i].setWidth(1000);
  dateEditTextAray[i].setHint("Date");
 //String datetag=new String();
   //     datetag="rid" +   requirement.r_id + "datetag";
    //    date1.setTag(datetag);
  dateEditTextAray[i].setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           positionClicked = j;
            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();
    dateEditTextAray[positionClicked].setText(nextdate);           
    }
}
};