【发布时间】:2016-04-24 11:29:59
【问题描述】:
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),它会随机显示对话框。但是如果数据库没有信息......“其他”不起作用。
不知道什么时候数据库没有信息,“mCursor”等于null???
【问题讨论】:
-
它总是正确的,因为应该返回一个条目......
-
在没有记录时调试游标值并相应修改您的 if 语句
-
尝试用
if(mCursor == null)代替esle -
嗨,您的查询总是返回一个值。不是吗?
标签: java android sqlite if-statement