简体   繁体   中英

Boolean method using contentprovider always returns true?

I have the following method for checking whether an input String already existed in database:

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

But no matter I input a new String or a String already existed, this method always return "true".

Was it a problem with the code or could it be a problem with the contentprovider?

Check if cursor.getCount() > 0. Do not check for null. The cursor can be empty and not null.

if(cursor != null && cursor.getCount() > 0) {
         t = true;
} else{
         t = false;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM