安卓:为什么OnDateChange回调被调用两次? [英] Android: why OnDateChange callback is called twice?

查看:317
本文介绍了安卓:为什么OnDateChange回调被调用两次?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的这一个点使用的DatePicker小部件的Andr​​oid应用程序,butI've注意到,每次我试图更改日期时,OnDateChange回调被调用了两次。
我可以忍受的,如果新的日期是在所有设备上保持一致,但我刚刚注意到,在模拟器首先是所谓的好日期,但第二次与旧的日期。

I'm working on an android app which at a point uses a DatePicker widget, butI've noticed that every time I try to change the date, the OnDateChange callback is called twice. I could live with that if the new date was consistent on all devices, but I've just noticed that on emulator first is called with the good date but the second time with the old date.

这是在回调中印日志:

12-18 20:29:12.285: E/MyOnDateChangeListener(5301): android.widget.DatePicker@43da7f90; year=2011; monthOfYear=11; dayOfMonth=19
12-18 20:29:12.355: E/MyOnDateChangeListener(5301): android.widget.DatePicker@43da7f90; year=2011; monthOfYear=11; dayOfMonth=18

我失去了一些东西在这里?

Am I missing something here?

请两次注意,从手机(Nexus S的)日志我有新的日期。

Please note that on the logs from the phone (Nexus S) both times I have the new date.

下面是监听器:

public class MyOnDateChangeListener implements OnDateChangedListener {
    @Override
    public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        Log.e("MyOnDateChangeListener", view.toString() + "; year=" + year + "; monthOfYear=" + monthOfYear + "; dayOfMonth=" + dayOfMonth);
        Date date = new Date();
        date.setDate(dayOfMonth);
        date.setMonth(monthOfYear);
        date.setYear(year - 1900);

        // TODO: modify the list content.
        ListEventsByDate(date);
    }
}

这里是创建/注册部分:

And here is the creation/register part:

    currentDate = new Date();
    currentDate.setDate(day);
    currentDate.setMonth(month);
    currentDate.setYear(year - 1900);

    MyOnDateChangeListener onDateChangeListener = new MyOnDateChangeListener();
    eventDatePicker.init(year, month, day, onDateChangeListener);

    try {
        eventDatePicker.setMinDate(currentDate.getTime() - DAY_IN_MILIS);
    } catch (Throwable e) {
        Log.e("ERR", "no MinDate method available");
    };

我忘了提,如果我写在文本框中它是确定的日期,我说的这个问题只有当我打的+和 - 。按钮

I forgot to mention that if I write the date in the text fields it is ok, the issue I'm talking about is only when I hit the '+' and '-' buttons.

推荐答案

我发现这个问题。这是因为我的code的。在ListEventsByDate()方法,我改变的DatePicker下面的列表上的焦点,并引发了第二个电话!

I found the problem. It was because of my code. In ListEventsByDate() method I was changing the focus on the list below the DatePicker and that triggered the second call!

这篇关于安卓:为什么OnDateChange回调被调用两次?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆