Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为什么我的光标返回一些不正确的信息?_Java_Android_Sqlite_Cursor - Fatal编程技术网

Java 为什么我的光标返回一些不正确的信息?

Java 为什么我的光标返回一些不正确的信息?,java,android,sqlite,cursor,Java,Android,Sqlite,Cursor,我正在制作一个应用程序,他们将一些信息输入SQLite数据库,然后使用ListView显示 不过好像有点bug,我找不到 示例:当他们输入第一个项目的信息并选择不拍照时,该项目在ListView中正确显示。它不会有一个画展,这是完美的。但是如果他们添加了另一个项目,并且这次他们选择拍照,那么第二个项目的图像也将显示在列表中的第一个项目中 而且,listview中的图像是数据库中的缩略图列 我有几张图片可以帮助展示这一点: 数据库信息的外观: 这就是它在应用程序中的样子。第一个项目不应显示缩略

我正在制作一个应用程序,他们将一些信息输入SQLite数据库,然后使用ListView显示

不过好像有点bug,我找不到

示例:当他们输入第一个项目的信息并选择不拍照时,该项目在ListView中正确显示。它不会有一个画展,这是完美的。但是如果他们添加了另一个项目,并且这次他们选择拍照,那么第二个项目的图像也将显示在列表中的第一个项目中

而且,listview中的图像是数据库中的缩略图列


我有几张图片可以帮助展示这一点:

数据库信息的外观:

这就是它在应用程序中的样子。第一个项目不应显示缩略图,而是显示第二个项目的缩略图:

我想不出来。我的代码看起来是正确的,该光标的所有其他信息都是正确的,但为什么只是缩略图不正确呢

以下是ListView使用的my CursorAdapter中的相关代码:

 //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);
    }
有人能猜到这里会发生什么吗


编辑:添加整个游标适配器代码:

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

}

}

您应该将
光标或适配器更改为:

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);
    }
致:


bitmap==null
分支还需要在
winembnail
上设置图像。这个视图回收错误可能不是您代码中的唯一错误-如果没有帮助,请发布更多适配器代码。@laalto我刚刚添加了整个CursorAdapter。我不敢相信解决方案会这么简单。我真的很喜欢有时候你在做一件小事的时候就钻到了这个兔子洞里。谢谢你,伙计!
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);
    }