gpt4 book ai didi

java - "else"不工作

转载 作者:行者123 更新时间:2023-12-02 04:01:11 25 4
gpt4 key购买 nike

public void randomFood (View view){
Cursor mCursor = database.rawQuery("SELECT name FROM member ORDER BY RANDOM() LIMIT 1", null);
if (mCursor != null) {
mCursor.moveToFirst();
String name = mCursor.getString(mCursor.getColumnIndex("name"));
Log.i("===============", name);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("This is your dog name");
dialog.setCancelable(true);
dialog.setMessage(name);
dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}

else{
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Wait!!");
dialog.setCancelable(true);
dialog.setMessage("Please add information");
dialog.setPositiveButton("Go", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Random.this, MainActivity.class);
startActivity(intent);
}
});
dialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
dialog.show();
}
}

当我点击按钮时。如果数据库有信息(!mCursor.equals(null)or(mCursor != null),它会随机并显示对话框。但是如果数据库没有信息...“else”不起作用.

不知道什么时候数据库没有信息,“mCursor”等于null???

最佳答案

这是因为总是返回一个非空值 - 甚至在您没有收到任何行的情况下(并且 SQL 不会抛出异常)

你最好重新编码你的 if

if(mCursor.getCount() > 0) {

关于java - "else"不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34869214/

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