Search code examples
javaandroidsqlitecursor

Why is my cursor returning some incorrect information?


I am making an app where they enter some information into a SQLite database and then it displays using a ListView.

There seems to be a bit of a bug though and I can't find it.

Example: When they are entering the info for the first item, and they choose to not take a picture, the item displays correctly in the ListView. It wont have a picture show, which is perfect. But if they add another item, and this time they choose to take a picture, the image for the second item will also show for the first item in the list.

And also, the image in the listview is the thumbnail column in the database.


I have a couple of pictures to help show this:

What the database info looks like:

What the database info looks like:

And this is what it looks like in the app. The first item should not have an thumbnail show, but instead it shows the thumbnail from the second item:

enter image description here

I can't figure this out. My code looks to be correct, and all of the other information for that cursor is correct, but why is just the thumbnail not?

Here is the relevant code in my CursorAdapter that the ListView uses:

 //This turns the thumbnail's bytearray into a bitmap
    byte[] bitmap = cursor.getBlob(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_THUMBNAIL));
    if (bitmap==null){
        Log.d(TAG, "No image was taken for wine: " + wineName);
    }else {
        Bitmap image = BitmapFactory.decodeByteArray(bitmap, 0, bitmap.length);
        wineThumbnail.setImageBitmap(image);
    }

Does anyone have any guesses on what could be going on here?


EDIT: Adding in the whole CursorAdapter code:

public class WineCursorAdapter extends CursorAdapter {

private String TAG = "WineCursorAdapter";

public WineCursorAdapter(Context context, Cursor c) {
    super(context, c);
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    return LayoutInflater.from(context).inflate(R.layout.list_item, parent, false);
}

@Override
public void bindView(View view, Context context, Cursor cursor) {
    //Populate the views
    TextView textViewName = (TextView) view.findViewById(wineName);
    TextView textViewPrice = (TextView) view.findViewById(R.id.winePrice);
    RatingBar ratingWine = view.findViewById(R.id.wineRating);
    ImageView wineThumbnail = view.findViewById(R.id.listImage);

    //get the info from cursor
    String wineName = cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_NAME));
    String winePrice = cursor.getString(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_PRICE));
    float wineRating = cursor.getFloat(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_RATING));

    //This turns the thumbnail's bytearray into a bitmap
    byte[] bitmap = cursor.getBlob(cursor.getColumnIndexOrThrow(WineContract.WineEntry.COLUMN_WINE_THUMBNAIL));
    if (bitmap==null){
        Log.d(TAG, "No image was taken for wine: " + wineName);
    }else {
        Bitmap image = BitmapFactory.decodeByteArray(bitmap, 0, bitmap.length);
        wineThumbnail.setImageBitmap(image);
    }

    textViewName.setText(wineName);
    DecimalFormat format = new DecimalFormat("####.##");
    String formatted = format.format(Float.parseFloat(winePrice));
    textViewPrice.setText("$" + formatted);
    ratingWine.setRating(wineRating);

}

}


Solution

  • You should change your CursorAdapter from:

    if (bitmap==null){
            Log.d(TAG, "No image was taken for wine: " + wineName);
        }else {
            Bitmap image = BitmapFactory.decodeByteArray(bitmap, 0, bitmap.length);
            wineThumbnail.setImageBitmap(image);
        }
    

    to:

    if (bitmap==null){
            Log.d(TAG, "No image was taken for wine: " + wineName);
            wineThumbnail.setImageBitmap(null);
        }else {
            Bitmap image = BitmapFactory.decodeByteArray(bitmap, 0, bitmap.length);
            wineThumbnail.setImageBitmap(image);
        }