我正在创建一个应用程序,他们将一些信息输入到SQLite数据库中,然后使用ListView进行显示。
虽然似乎有一点虫子但我找不到它。
示例:当他们输入第一个项目的信息,并且他们选择不拍照时,该项目在ListView中正确显示。它没有图片展示,这是完美的。但是如果他们添加了另一个项目,并且这次他们选择拍照,则第二个项目的图像也将显示在列表中的第一个项目。
此外,listview中的图像是数据库中的缩略图列。
我有几张照片可以帮助证明这一点:
数据库信息是什么样的:
这就是它在应用程序中的样子。第一项不应该有缩略图显示,而是显示第二项的缩略图:
我无法弄清楚这一点。我的代码看起来是正确的,并且该光标的所有其他信息都是正确的,但为什么只是缩略图?
以下是ListView使用的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);
}
有没有人对这里发生的事情有任何猜测?
编辑:添加整个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);
}
}
答案 0 :(得分:1)
您应该更改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);
}