gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:19:08 25 4
gpt4 key购买 nike

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

虽然似乎有一点错误,但我找不到它。

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

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


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

数据库信息是什么样的:

What the database info looks like:

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

enter image description here

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

这是我的 CursorAdapter 中 ListView 使用的相关代码:

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

有人猜到这里会发生什么吗?


编辑:添加整个 CursorAdapter 代码:

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

}

最佳答案

您应该将 CursorAdapter 更改为:

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

到:

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

关于java - 为什么我的光标返回一些不正确的信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46634032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com