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”等于空???

最佳答案

你需要改变

if (mCursor != null)



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

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

关于java - “其他”不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34869214/

10-09 07:23