我有两个单独的课程。一个是我的可扩展的循环视图,另一个是我的 datePickerDialog 类。我的可扩展子视图中有3个textview。
我试图从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();
}
答案 0 :(得分:0)
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);
上述方法提供日,月和年
答案 1 :(得分:0)
返回值不是此方案中的解决方案。如果你想编写可重用代码,那么使用回调(Inteface或Abstact类)。你的问题可以通过回调来解决。请看下面的代码: -
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()方法的结果。您可以使用匿名回调或单个回调(通过DateCallBack实现您的类)。