2015-12-09 41 views
0

嗨我想在第一次用户打开对话框时设置日期选择器对话框上的特定日期(从服务器)。之后,日期将被动态设置。但我无法在日期选择器上获得该日期。它总是显示当前的最短日期。有没有简单的方法来解决这个问题?从datePicker对话框的服务器获取特定日期android

//My date from server 

     Calendar cal = Calendar.getInstance(); 
      long l = Long.parseLong(posttime); 
        cal.setTimeInMillis(l); 
        int dmonth = cal.get(Calendar.DAY_OF_MONTH); 
        int dday = cal.get(Calendar.MONTH); 
        int dyear= cal.get(Calendar.YEAR); 
      dateTextView.setText(dday+" " + dmonth + " " +dyear); 

     //Date to be set dynamically 

     Calendar now = Calendar.getInstance(); 
     DatePickerDialog dpd = DatePickerDialog.newInstance(
        PostInfoUpdate.this, 
        now.get(Calendar.YEAR), 
        now.get(Calendar.MONTH), 
        now.get(Calendar.DAY_OF_MONTH)+1 
      ); 

     dpd.setMinDate(Calendar.getInstance()); 
       now.add(Calendar.DAY_OF_MONTH, 30); 
       dpd.setMaxDate(now); 


     //To open the dialog 
       public void show() { 
      dpd.show(getFragmentManager(), "Datepickerdialog"); 

       } 

     //To set the date 
      @Override 
     public void onDateSet(DatePickerDialog view, int year, int monthOfYear, int dayOfMonth) { 
     String date = "You picked the following date: "+dayOfMonth+"/"+(++monthOfYear)+"/"+year; 
      dateTextView.setText(date); 
      } 

回答

0

如果你不希望当前的时间要设置不这样做;)

 dpd.setMinDate(Calendar.getInstance()); 
      now.add(Calendar.DAY_OF_MONTH, 30); 
      dpd.setMaxDate(now); 

定义自己的“时间”(从服务器或静态),并设置该

编辑:

@Override 
protected Dialog onCreateDialog(int id) 
{ 
    switch (id) { 
     case DATE_DIALOG_ID: 
      date_picker_dialog = new DatePickerDialog(this, get_date, year, month - 1, date); 
      return date_picker_dialog; 
    } 
} 
+0

能否请您解释清楚? – spykeburn

+0

http://stackoverflow.com/questions/26310750/how-to-set-a-specific-date-in-date-picker-in-android你可以使用答案1中的方法将datepicker onstart设置为你想要的日期。我“引用”的代码将日期设置为当前日期(现在)。我希望这有帮助。 – Rastaman

+0

我试过这个dpd.initialize(mCallBack,dyear,dmonth,dday);我得到了我的约会,但现在我无法更改日期。 – spykeburn

0

如果要插入一个自定义的日期,当你从服务器得到了你的约会将其切分为“年” - “月” - “天”和插入它们这个方法

public dinamicDate(int year, int month, int day){ 
    Calendar now = Calendar.getInstance(); 
       DatePickerDialog dpd = DatePickerDialog.newInstance(
         Context, 
         year, 
         month, 
         day; 
       dpd.show(getFragmentManager(), "Datepickerdialog"); 
    } 

,那么你只需要调用它

dinamicDate(year, month, day); 
相关问题