在静态类中填充非静态变量(Spinner)

时间:2014-10-12 16:20:15

标签: android variables static non-static

我正在努力做到这一点:

  1. 在DatePickerDialog用户设置日期
  2. 日期将以Spinner编写
  3. 问题是包含OnDateSet方法的DatePickerFragment类是静态的,而微调器是非静态的。在OnDateSet方法中我需要将项添加到适配器并调用方法Spinner.setSelection(int i)

    public static class DatePickerFragment extends DialogFragment
                                implements DatePickerDialog.OnDateSetListener {
    
               @Override
               public Dialog onCreateDialog(Bundle savedInstanceState) {
                   // Use the current date as the default date in the picker
                   final Calendar c = Calendar.getInstance();
                   int year1 = c.get(Calendar.YEAR)-25;
                   int month1 = 0;
                   int day1 = 1;
    
    
                   // Create a new instance of DatePickerDialog and return it
                   return new DatePickerDialog(getActivity(), this, year1, month1, day1);
               }
    
               public void onDateSet(DatePicker view, int year1, int month1, int day1) {
                   firstdate = day1 + "/" + month1 + "/" + year1;
               }
    
           }
    

    错误在这一行:

    firstdate = day1 + "/" + month1 + "/" + year1;
    

    有人能帮助我吗?

    提前致谢。

1 个答案:

答案 0 :(得分:0)

你也可以在片段中声明它,并有一个返回其值的getter;

public static class DatePickerFragment extends DialogFragment
                        implements DatePickerDialog.OnDateSetListener {

 private int firstdate;

 // .. other code

 public int getFirstDate() {
    return firstdate;
 }

}

从您的活动onDissmiss开始,您可以将其称为mDialogInstance.getFirstDate();