2014-02-24 66 views
0

的日期選擇器對話框值,我需要日期選擇器對話框中返回選定的值,看看我下面編碼,需要返回的Android

這是我已經使用日期選擇器對話框

if (holder.mClientNextvisit.isChecked() == true) { 
    holder.SelectDateLayout.setVisibility(View.VISIBLE); 

    holder.mClientSelectDate.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      Calendar calender = Calendar.getInstance(); 
      year = calender.get(Calendar.YEAR); 
      month = calender.get(Calendar.MONTH); 
      day = calender.get(Calendar.DAY_OF_MONTH); 

      Log.d("Selected date", "---->" + day); 

      DatePickerDialog dialog = new DatePickerDialog(context, new DateListener(), year, month, day); 
      dialog.show(); 

     } 
    }); 

} 
我的適配器類

這是我datesetlistener類

class DateListener implements DatePickerDialog.OnDateSetListener { 

@Override 
public void onDateSet(DatePicker view, int year, int monthOfYear, 
     int dayOfMonth) { 
    // TODO Auto-generated method stub 
    int mYear = year; 
    int mMonth = monthOfYear; 
    int mDay = dayOfMonth; 
    StringBuilder date = new StringBuilder().append(mMonth + 1) 
      .append("/").append(mDay).append("/").append(mYear) 
      .append(" "); 

    Log.d("date", "---->" + date); 

} 

現在我需要返回適配器類選定的日期,請^ h elp我。

+1

您可以將日期存儲在靜態字符串或sharedpreference中,然後在您想要的任何地方使用。 –

回答

2

如何使用匿名類?

DatePickerDialog dialog = new DatePickerDialog(context, 
    new OnDateSetListener() { 

     @Override 
     public void onDateSet(DatePicker arg0, int arg1, int arg2, 
      int arg3) { 
      int mYear = year; 
      int mMonth = monthOfYear; 
      int mDay = dayOfMonth; 
      StringBuilder date = new StringBuilder() 
       .append(mMonth + 1).append("/").append(mDay) 
       .append("/").append(mYear).append(" "); 

      Log.d("date", "---->" + date); 

     } 
    }, year, month, day); 
1

假設mHolderTextView傳遞引用到您的收聽,並直接使用它。

class DateListener implements DatePickerDialog.OnDateSetListener { 
    private TextView mHolder; 
    public DateListener(TextView mHolder) { this.mHolder = mHolder; } 
    @Override 
    public void onDateSet(DatePicker view, int year, int monthOfYear, 
     int dayOfMonth) { 
    // TODO Auto-generated method stub 
    int mYear = year; 
    int mMonth = monthOfYear; 
    int mDay = dayOfMonth; 
    StringBuilder date = new StringBuilder().append(mMonth + 1) 
      .append("/").append(mDay).append("/").append(mYear) 
      .append(" "); 

    Log.d("date", "---->" + date); 
    mHolder.setText(date); 
} 

DatePickerDialog dialog = new DatePickerDialog(
context, new DateListener(mHolder), year,month, day); 
dialog.show();