Java &引用;否则";不起作用

Java &引用;否则";不起作用,java,android,sqlite,if-statement,Java,Android,Sqlite,If Statement,当我点击一个按钮。若数据库有一个信息(!mCursor.equals(null)或(mCursor!=null),它将随机显示对话框。但若数据库并没有信息…“else”不起作用 我不知道数据库什么时候没有信息,“mCursor”等于null rawQuery()将返回游标对象或抛出 异常。它永远不会返回null 相反,试试这个 public void randomFood (View view){ Cursor mCursor = database.rawQuery("SELECT na

当我点击一个按钮。若数据库有一个信息
(!mCursor.equals(null)或(mCursor!=null)
,它将随机显示对话框。但若数据库并没有信息…“else”不起作用

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

rawQuery()将返回游标对象或抛出 异常。它永远不会返回null

相反,试试这个

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();
    }
}
rawQuery()将返回游标对象或抛出 异常。它永远不会返回null

相反,试试这个

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();
    }
}

这是因为总是返回一个非空值——即使在没有收到行的情况下(并且SQL不会抛出
异常

如果你愿意,你最好重新编码

if(mCursor.moveToFirst())
{

}
else
{

}

这是因为总是返回一个非空值——即使在没有收到行的情况下(并且SQL不会抛出
异常

如果你愿意,你最好重新编码

if(mCursor.moveToFirst())
{

}
else
{

}
你需要改变

if(mCursor!=null)

if(mCursor!=null&&mCursor.moveToFirst())

这是因为游标可以为空,即您的查询已成功执行,但所选行数为0。

您需要更改

if(mCursor!=null)

if(mCursor!=null&&mCursor.moveToFirst())


这是因为游标可以为空,即您的查询已成功执行,但所选行数为0。

这始终是真的,因为应返回一个条目…在没有记录时调试游标值,并根据尝试
if(mCursor==null)修改if语句
代替
esle
可能重复的hi,您的查询总是返回一个值。不是吗?总是正确的,因为应该返回一个条目…在没有记录时调试光标值,并根据尝试
if(mCursor==null)修改if语句
您的查询总是返回一个值,而不是
esle
可能重复的hi,不是吗?