-1

I have two separate class. One is my expandable recycleview and the other one is my datePickerDialog class.I have 3 textview inside my expandable child view.

I tried to return the value from the DatePickerDialog, but it said that the method is read-only, When I tried to re-assign it to another variable outside of it, and try to return the value to the adapter.. the adapter would only be able to read it after the second time i picked the date. (In this case, it showed the first date). Is there a way for me to correctly return the date value or at least wait for the value retrieved from the picker.

The textview showed it, but i wasn't able to assign it to array. Thus resulted the child view to loose it value one the parent collapse.

adapter

@Override
    public void onBindChildViewHolder(final NotisChildViewHolder notisChildViewHolder, int i, Object childObject) {
        final NotisChild notisChild = (NotisChild) childObject;

        notisChildViewHolder.mItem.setText(notisChild.getItem());
        notisChildViewHolder.mDate.setText(notisChild.getDate());
        notisChildViewHolder.mTime.setText(notisChild.getTime());

//        todo: take value of time & date in childview.
        notisChildViewHolder.mDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Utility.onDateSelected(notisChildViewHolder.mDate, notisChild.getDate(), mContext);
                Log.d(TAG, "onClick: notisChildViewHolder.mDate "+ notisChildViewHolder.mDate.getText().toString());
                notisChild.setDate(notisChildViewHolder.mDate.getText().toString());
            }
        });
}

datePicker class

public static void onDateSelected(final TextView textView, String dateText, Context mContext) {
        Log.d(TAG, "onDateSelected");
        Date date = null;
        final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault());

        try {
            if (dateText.equals(""))
                date = new Date();
            else
                date = simpleDateFormat.parse(dateText);
        } catch (Exception exp) {
            // In case of expense initializa date with new Date
            date = new Date();
        }

        final Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH); // calendar month 0-11
        int day = calendar.get(Calendar.DATE);

        // date picker initialization
        DatePickerDialog datePicker = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int month, int day) {
                calendar.set(year, month, day);
                String formattedDate = simpleDateFormat.format(calendar.getTime());
                textView.setText(formattedDate);
            }
        }, year, month, day);

        datePicker.show();
    }

2 Answers2

1

onDateSet() method provide you the selected date. As you have already used that method just do the following changes.

DatePickerDialog datePicker = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int year, int month, int day) {

            String formattedDate = day+"/"+month+"/"+year;
        }
    }, year, month, day);

Day,month and year are provided from the the above method

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
  • but i still can't get the value for the formatted date... i still have to set the date, because then, it would only take the current date... instead of user input – Hibiki Ryou Nov 03 '17 at 01:06
0

Returning the value is not the solution in this scenario. If you want to write Reusable code then use callbacks(Inteface or Abstact class) .The problem of your can be solved by an callback . Here see the code below:-

 public class DatePicker implements DatePickerDialog.OnDateSetListener {
    private DatePickerDialog pickerDialog;
    private static final SimpleDateFormat dateFomate = new SimpleDateFormat("dd/MMM/yyyy");
    private DateCallBack callBack;

    public DatePicker(Context context, DateCallBack callBack, String preDate, int type) {
        this.callBack = callBack;
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(System.currentTimeMillis());
        pickerDialog = new DatePickerDialog(context, this, calendar.get(Calendar.YEAR), calendar.get(calendar.MONTH)
                , calendar.get(calendar.DAY_OF_MONTH));
        pickerDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        pickerDialog.getDatePicker().setDescendantFocusability(android.widget.DatePicker.FOCUS_BLOCK_DESCENDANTS);
        pickerDialog.setTitle("");
        if (type == TYPE_DOB) {
            Calendar cal = Calendar.getInstance();
            cal.add(Calendar.YEAR, -18);
            pickerDialog.getDatePicker().setMaxDate(cal.getTimeInMillis());
        } else {
            Calendar cal = Calendar.getInstance();
            cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            pickerDialog.getDatePicker().setMinDate(cal.getTimeInMillis());
        }
        pickerDialog.setTitle("");
    }

    public void show() {
        pickerDialog.show();
    }


    @Override
    public void onDateSet(android.widget.DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, monthOfYear);
        calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        if (callBack != null)
            callBack.onDateSet(dateFomate.format(calendar.getTimeInMillis()), calendar.getTimeInMillis());
        pickerDialog.dismiss();
    }

    public interface DateCallBack {
        void onDateSet(String date, long timein_millies);
    }


}

DateCallBack willl work as callback you will get Result on its onDateSet() method. You can use anonymous callback or a single( by implements your class by DateCallBack).

ADM
  • 20,406
  • 11
  • 52
  • 83