0

嘿,我这里有问题。

我正在尝试根据传递给方法的日期从数据库中检索日期,并返回该值。

问题是,当我尝试传递接收日期的变量时,从数据库中选择数据的方法没有返回任何内容。(我在 LogCat 上打印日期变量,没关系,日期值是正确的),但如果我传递这样的字符串值(“1/01/1111”),它会正确返回。

这是获取值并设置文本的活动的方法。

public void setBasicContent() {

    date = (mMonth + 1) + "/" + mDay + "/" + mYear + " ";
    hpdData = this.hpd.selectDuration(date);
    mDateDisplay.setText(hpdData);

}

这是 selectDuration() 方法,它根据日期参数从数据库中选择数据。

啊,当我在活动中传递变量日期时,代码没有到达if(cursor.moveToFirst())范围。但我不知道为什么,因为变量值完全正确地与普通字符串完全一样。

public String selectDuration(String date) {

    String duration = "";
    Integer value = 0;
    String returnment = "";
    Log.i(TAG, "date to select: " + date);
    Cursor cursor = this.db.query(TABLE_NAME, new String[] { "duration" },
            "date = ?", new String[] { date }, null, null, null);

    if (cursor.moveToFirst()) {
        do {
            Log.i("SELECTDURATION", "inside cursor.moveToFirst()");
            duration = cursor.getString(0);
            value += Integer.parseInt(duration);

        } while (cursor.moveToNext());
        returnment = Integer.toString(value);
    }

    if (cursor != null && !cursor.isClosed()) {
        cursor.close();
    }
    Log.i(TAG, "valor do returnment: " + returnment);
    return returnment;
}
4

3 回答 3

1

我发现了错误。它在setBasicContent()方法上。

这是旧方法:

public void setBasicContent() {

date = (mMonth + 1) + "/" + mDay + "/" + mYear + " ";
hpdData = this.hpd.selectDuration(date);
mDateDisplay.setText(hpdData);

}

这是修改后的新方法:

public void setBasicContent() {

date = (mMonth + 1) + "/" + mDay + "/" + mYear;
hpdData = this.hpd.selectDuration(date);
mDateDisplay.setText(hpdData);

}

问题出在这一行:

 date = (mMonth + 1) + "/" + mDay + "/" + mYear + " ";

如果您看到,它将一个空字符连接到日期,因此在 String 中将传递带有空字符的日期字符串,这对于 String 有所不同。所以它必须是这样的:

date = (mMonth + 1) + "/" + mDay + "/" + mYear;
于 2011-02-04T23:14:19.180 回答
0

我已经在我的博客上写了我的方法。我转换为日历对象并使用 getTimeInMillis()。这样,您只需在数据库中存储一个“长”值。

在 SQLite 数据库中存储 DatePicker 和 TimePicker

于 2012-02-29T13:19:08.030 回答
0

如果您的数据库引用实例化正常,我在您的代码中看不到任何明显不正确的内容。如果可能,将日期作为整数存储在数据库中,将毫秒值存储在 date.toTime() 中。然后从 new Date(milliseconds) 实例化一个日期对象要容易得多,然后您可以根据语言环境格式化输出。

于 2011-02-04T20:11:28.243 回答