2017-10-08 36 views
1

我正在製作一個應用程序,他們在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); 

} 

}

+0

「位圖== null」分支也需要在'wineThumbnail'上設置圖像。此視圖回收錯誤可能不是您代碼中的唯一一個 - 如果這沒有幫助,請發佈更多適配器代碼。 – laalto

+0

@laalto我剛添加在整個CursorAdapter中。 – andrdoiddev

回答

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

我不能相信解決方案很簡單。當你只是做這個小小的事情的時候,總是愛你有時候會把整個兔子洞放下來。非常感謝! – andrdoiddev

相關問題