irobotxx
irobotxx

Reputation: 6063

sqlite query to retrieve all rows where not null android

Good day, I have like 2 question here:

First a background information.

from the android documentation, when querying a database, we have to return a column with name "_id" for it to work with a cursor adapter. Now in my implementation, lets say i have 3 columns "_id", "column A" "column B".

so if i have 8 entry rows in my database table, where the 1st 3 rows in column A is filled and the next 5 rows in column B is filled. if i query the database for column B and use the code below in my bindview, the cursor returns all the rows which will result in the first 3 entries being null and the last 5 items being shown in my gridview of images.

here is my bindView:

@Override
public void bindView(View view, Context context, Cursor cursor) {
     ViewHolder holder = (ViewHolder)view.getTag();
 int columnIndex = cursor.getColumnIndexOrThrow(columnName);
 final String name = cursor.getString(columnIndex);
     while(cursor.moveToNext()){

            if(name != null){
             Log.d(TAG, "string name in gridimageadapter is " + name);

             BitmapFactory.Options options = new BitmapFactory.Options();
             Bitmap bitmap = BitmapFactory.decodeFile(name);

             Bitmap newbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, false);
             if(holder.image != null){
                 holder.image.setImageBitmap(newbitmap); 
             }
             bitmap.recycle();

            }
     }
}

so my questions are:

  1. How do i write an sqlite query to return a column with rows not null? my current implementation is:

    public Cursor retrieveTag(String tag){

        String[] condition = {KEY_ID,tag};
    Cursor cursor = db.query(DATABASE_TABLE, condition, null, null, null, null, null);      
        return cursor;
    }
    
  2. from the BindView code, if i use this condition like

    if( !name.equals(null) || !name.equals("")){ }

i get a NullPointerException on the it. Why is that?.. this is the correct way to test for Strings right?..

Hope am clear in my question and any help will be greatly appreciated. Thank you.

Upvotes: 4

Views: 7925

Answers (1)

koti
koti

Reputation: 3701

By using Sql query you can manage the data without null values see below code.

Cursor c=db.rawQuery("Select columnB from tablename where columnB IS NOT NULL");

Upvotes: 7

Related Questions