gpt4 book ai didi

java - SQLite 游标条件出错——需要第二双眼睛

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:16 25 4
gpt4 key购买 nike

所以这就是我的问题 - 代码应该很简单,但它不起作用,我不确定为什么。下面是我为名为 NEXT 的按钮设置的代码,用户在填写完日期和锻炼信息后点击该按钮。如果输入的日期已经在数据库中,他们应该收到 Toast 消息提醒,并且不允许继续到下一页。但是,发生的情况是对输入日期的数据库查询显然失败了,或者我设置的条件以某种方式阻止了执行,或者我对 Cursor 做错了什么。

注意主要问题中的条件 -- if (date_matches.getCount() > 0 && date_matches != null){//告诉用户在 MODIFY 选项卡中输入新日期或修改旧日期

我现在用 && 得到了它,这会导致现有和不存在的日期都进入下一个 Activity 。如果我将其更改为 ||,执行永远不会到达“else” block ,即使数据库中尚不存在该日期。

在这个讨厌的小问题上,任何朝着正确方向的努力都会受到赞赏!

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0){

String date = date_entry.getText().toString();
String exercise = date_entry.getText().toString(); //this is not required -- can modify day to add later

//If date field left empty but NEXT button pressed, issue a toast message telling user to enter one
if (date == null || date.length() == 0){
CharSequence message = "No date given. Please enter one before pressing NEXT";
Toast toast = Toast.makeText(context, message, duration);
toast.show();}

//If exercise left empty but NEXT button pressed, will just fill exercise with 'none', which can be
//later modified if user exercises after they create their new date
if (exercise == null || exercise.length() == 0){
exercise = "None";
}

String query = "SELECT * FROM food_entries WHERE date = "+date;

Cursor date_matches = mDbHelper.getData(query);

//There is a problem with this conditional -- even when there should be no matching rows for
//a new date, this is coming up with some sort of result and letting it pass
if (date_matches.getCount() > 0 && date_matches != null){
//Matching date in food_entries, meaning foods for that date already inputted
CharSequence message = "Information for "+date+" already exists. Add/modify in MODIFY tab";
Toast toast = Toast.makeText(context, message, duration);}

else{
//No matching dates; can add new date
Intent intent = new Intent(context, AddFoodsActivity.class);
intent.putExtra("date_key", date);
intent.putExtra("exercise_key", exercise);
startActivity(intent);}

}});

最佳答案

您的getData 函数可能会调用queryrawQuery。这些函数从不返回 null;当他们什么也没找到时,他们返回一个包含零项的有效游标。(只有内容提供者可能会返回 null 而不是空游标。)

只需删除 date_matches != null 检查即可。

关于java - SQLite 游标条件出错——需要第二双眼睛,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23188016/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com