我有以下方法来检查数据库中是否已存在输入字符串:
public boolean check(String s){
boolean t = false;
ContentResolver contentResolver = this.getContentResolver();
Cursor cursor = contentResolver.query(FTagsContentProvider.CONTENT_URI,
new String []{FTagsContentProvider.COL_TEXT}, FTagsContentProvider.COL_TEXT + "=?", new String[]{s}, null);
if(cursor != null){
t = true;
}
else{
t = false;
}
cursor.close();
return t;
}
但是无论我输入一个新的字符串还是一个已经存在的字符串,这个方法总是返回“true”。
是代码有问题还是内容提供者有问题?
最佳答案
检查cursor.getCount() > 0。不检查是否为null。光标可以为空,也可以不为空。
if(cursor != null && cursor.getCount() > 0) {
t = true;
} else{
t = false;
}
关于java - 使用 contentprovider 的 boolean 方法总是返回 true?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30745825/