为什么我的光标返回一些不正确的信息?
Why is my cursor returning some incorrect information?
我正在制作一个应用程序,他们将一些信息输入到 SQLite 数据库中,然后使用 ListView 显示。
不过好像有点bug,我没找到。
示例:当他们输入第一个项目的信息时,他们选择不拍照,该项目在 ListView 中正确显示。它不会有图片展示,这是完美的。但如果他们添加另一个项目,并且这次他们选择拍照,则第二个项目的图像也会显示列表中的第一个项目。
另外,列表视图中的图像是数据库中的缩略图列。
我有几张图片可以帮助展示:
数据库信息是什么样的:
这就是它在应用程序中的样子。第一项不应显示缩略图,而是显示第二项的缩略图:
我想不通。我的代码看起来是正确的,该光标的所有其他信息都是正确的,但为什么只有缩略图不正确?
这是我的 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);
}
我正在制作一个应用程序,他们将一些信息输入到 SQLite 数据库中,然后使用 ListView 显示。
不过好像有点bug,我没找到。
示例:当他们输入第一个项目的信息时,他们选择不拍照,该项目在 ListView 中正确显示。它不会有图片展示,这是完美的。但如果他们添加另一个项目,并且这次他们选择拍照,则第二个项目的图像也会显示列表中的第一个项目。
另外,列表视图中的图像是数据库中的缩略图列。
我有几张图片可以帮助展示:
数据库信息是什么样的:
这就是它在应用程序中的样子。第一项不应显示缩略图,而是显示第二项的缩略图:
我想不通。我的代码看起来是正确的,该光标的所有其他信息都是正确的,但为什么只有缩略图不正确?
这是我的 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);
}