从DatePicker更新TextView

时间:2016-03-14 19:32:36

标签: java android function static datepicker

我正在尝试从Datepicker内部更新TextView,但是我收到了错误

"非静态方法(updateDateTextView(long))无法从静态上下文引用"

这是我的DatePickerFragment.java

public 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 year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        AddItem.updateDateTextView(timestamp);
    }
}

这是我在AddItem.java中的一个Activity,它是一个Activity。

      public  class AddItem extends AppCompatActivity {

    Context ctx = this;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_item);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

    }
    public void showDatePickerDialog(View v) {
        DialogFragment newFragment = new DatePickerFragment();
        newFragment.show(getFragmentManager(), "datePicker");
    }

    public void updateDateTextView(long timestamp){
        TextView textViewToChange = (TextView) findViewById(R.id.openDate);
        Calendar cal = Calendar.getInstance(Locale.ENGLISH);
        cal.setTimeInMillis(timestamp);
        String date = DateFormat.format("dd-MM-yyyy", cal).toString();
        textViewToChange.setText(date);
    }

}

4 个答案:

答案 0 :(得分:2)

正如他们所说,你不能像静态方法一样调用非静态方法。试试这个:

在您的活动中实现DatePickerDialog.OnDateSetListener和逻辑:

public class AddItem extends AppCompatActivity implements DatePickerDialog.OnDateSetListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);


    showDatePickerDialog();
}

public void showDatePickerDialog() {
    DatePickerFragment newFragment = new DatePickerFragment();
    newFragment.init(this, this);
    newFragment.show(getSupportFragmentManager(), "date_picker");
}

public void updateDateTextView(long timestamp){
    TextView textViewToChange = (TextView) findViewById(R.id.openDate);
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(timestamp);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    textViewToChange.setText(date);
}

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

    Calendar calendar = new GregorianCalendar(year, monthOfYear, dayOfMonth);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());

        updateDateTextView(timestamp);
}

使用监听器初始化DatePicker,该类应如下所示:

public static class DatePickerFragment extends DialogFragment {

    DatePickerDialog.OnDateSetListener listener;
    Context context;

    public void init(Context context, DatePickerDialog.OnDateSetListener listener) {
        this.listener = listener;
        this.context = context;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(context, listener, year, month, day);
    }

}

我试过了,它确实有效。

答案 1 :(得分:0)

你这样做:

public void onDateSet(DatePicker view, int year, int month, int day) {
        Calendar calendar = new GregorianCalendar(year, month, day);
        long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
        AddItem.updateDateTextView(timestamp); ///Here is the error
    }

错误是 AddITem.updateDateTextView 错误。方法updateDateTextView不是静态的。

您需要使用Intent启动该Activity,并且生命周期中的某个位置调用方法updateDateTextView

修改方法并使其静态(但这不是很好的开发者想法)

public static void onDateSet(DatePicker view, int year, int month, int day)

答案 2 :(得分:0)

非静态方法不能与静态类一起使用 尝试改变

public void updateDateTextView(long timestamp){ ..... }

public static void updateDateTextView(long timestamp){
...}

答案 3 :(得分:0)

或者,可以在DatePickerFragment类中定义interface,如下面的代码所示。

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {

DateChooserListenerHandler dateChooserListenerHandler;
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the current date as the default date in the picker
    final Calendar c = Calendar.getInstance();
    int year = c.get(Calendar.YEAR);
    int month = c.get(Calendar.MONTH);
    int day = c.get(Calendar.DAY_OF_MONTH);

    // Create a new instance of DatePickerDialog and return it
    return new DatePickerDialog(getActivity(), this, year, month, day);
}
@Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    Calendar calendar = new GregorianCalendar(year, month, day);
    long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
    //Note this is where the updateTextView method in the interface is invoked from
    ((DateChooserListenerHandler) getActivity()).updateTextView(timestamp);
}

//Define an interface with a method that takes long, it will be overridden by classes that implement the interface
public interface DateChooserListenerHandler{

    public void updateTextView(long s);
}

}

然后,AddItem活动类可以像下面的代码一样实现接口。

public class AddItem extends AppCompatActivity implements DatePickerFragment.DateChooserListenerHandler {


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

}

public void showDatePickerDialog(View v) {
    DialogFragment newFragment = new DatePickerFragment();
    newFragment.show(getSupportFragmentManager(), "timePicker");
}

//The overridden method from the interface defined in DatePickerFragment class
@Override
public void updateTextView(long s) {
    updateDateTextView(s);
}

public void updateDateTextView(long timestamp){
    TextView textViewToChange = (TextView) findViewById(R.id.textView);
    Calendar cal = Calendar.getInstance(Locale.ENGLISH);
    cal.setTimeInMillis(timestamp);
    String date = DateFormat.format("dd-MM-yyyy", cal).toString();
    textViewToChange.setText(date);
}

}

然而,这对我来说很好,我没有检查它对应用程序的性能影响,但我发现它更容易。 如果您只是想直接从DatePickerFragment更新TextView,那么您可以像下面那样进行。

 @Override
public void onDateSet(DatePicker datePicker, int year, int month, int day) {
    Calendar calendar = new GregorianCalendar(year, month, day);
    long timestamp = TimeUnit.MILLISECONDS.toSeconds(calendar.getTimeInMillis());
    TextView textView = (TextView) getActivity().findViewById(R.id.textView);
    textView.setText(timestamp + "");
}