Search code examples
androidandroid-cursor

How to display "No results found" on search activity when the query dosen't matches with any result


I have implemented search on my application , it is working . But when the query string dosen't matches with anything it gives me blank screen , instead of that blank screen i want to display "No results found"

Here is my code for searching

public Cursor getSearchResults1(String query) {
    // TODO Auto-generated method stub
    String[] args=new String[]{query};
    return(getReadableDatabase().rawQuery("SELECT _id,section,section_name FROM sections WHERE section_name LIKE '%' || ? || '%'", args));
}

Please help me in this

Thanks In Advance!


Solution

  • Try this.

    if(cursor.moveToFirst()){
    // Do with the search result 
    }else{
    // no result
    }
    

    you can use cursor.getCount() but it is more computation expensive as it returns the number of rows for that query, more the number of row more time it will take. Its better to use cursor.moveToFirst() if you just want to know whether you got any data for query or not.