【问题标题】:How retrieve the value from datePickerDialog?如何从 datePickerDialog 中检索值?
【发布时间】:2018-04-14 18:24:11
【问题描述】:

我有两个单独的班级。一个是我的可扩展回收视图,另一个是我的 datePickerDialog 类。我的可扩展子视图中有 3 个文本视图。

我试图从 DatePickerDialog 返回值,但它说该方法是只读的,当我试图将它重新分配给它之外的另一个变量时,并尝试将值返回给适配器..适配器只能在我第二次选择日期后才能读取它。 (在这种情况下,它显示了第一个日期)。有没有办法让我正确返回日期值或至少等待从选择器中检索到的值。

textview 显示了它,但我无法将它分配给数组。因此导致子视图失去它的价值,父崩溃。

适配器

@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 类

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();
    }

【问题讨论】:

标签: android


【解决方案1】:

在这种情况下,返回值不是解决方案。如果您想编写可重用代码,请使用回调(接口或抽象类)。您的问题可以通过回调来解决。这里看下面的代码:-

 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 将作为回调工作,您将在其 onDateSet() 方法上获得 Result。您可以使用匿名回调或单个回调(通过 DateCallBack 实现您的类)。

【讨论】:

  • 如何调用回调?
【解决方案2】:

onDateSet() 方法为您提供选定的日期。由于您已经使用过该方法,因此只需进行以下更改。

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);

通过上述方法提供日、月、年

【讨论】:

  • 但我仍然无法获取格式化日期的值...我仍然需要设置日期,因为那样的话,它只会采用当前日期...而不是用户输入
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多