Android 数据库中的所有图像加载到第一个ListView错误
我从数据库中提取数据,各种字符串和表示外部目录中图像路径的字符串 我的问题是,当列表视图加载来自游标对象的所有图像时,这些图像都会加载到第一个列表视图中,这很耗时,然后每个图像都会正确加载到后续视图中 所以我的问题是,为什么数据库中的每个图像都被加载到第一个列表视图中,而不是按照要求每个列表视图加载一个图像 这是我的游标适配器代码,我使用异步任务从DB加载图像(提前向分配的代码道歉!) 欢迎您的任何意见Android 数据库中的所有图像加载到第一个ListView错误,android,listview,android-viewholder,Android,Listview,Android Viewholder,我从数据库中提取数据,各种字符串和表示外部目录中图像路径的字符串 我的问题是,当列表视图加载来自游标对象的所有图像时,这些图像都会加载到第一个列表视图中,这很耗时,然后每个图像都会正确加载到后续视图中 所以我的问题是,为什么数据库中的每个图像都被加载到第一个列表视图中,而不是按照要求每个列表视图加载一个图像 这是我的游标适配器代码,我使用异步任务从DB加载图像(提前向分配的代码道歉!) 欢迎您的任何意见 @Override public void bindView(View v
@Override
public void bindView(View v, Context context, final Cursor c) {
/*
* Binds the data from each row (stored in cursor object) to the ListView.xml
* first free up List object if not in view of the screen by holding a ref to it, therefore dont
* waste memeory and tiome recreating each view when out of viewable list to user
*/
ViewHolder holder = (ViewHolder) v.getTag();
String diveSite = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY_DIVESITE));
holder.title_text.setText(diveSite);
String date = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY__DIVEDATE));
holder.date_text.setText(date);
String rating = c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY_DIVERATING));
holder.bar.setNumStars(5);
holder.bar.setRating( Float.parseFloat(rating));
String diveNumber= c.getString(c.getColumnIndexOrThrow(diveDataBase.KEY__DIVENUMBER));
String diveImagePath = c.getString(c.getColumnIndex(diveDataBase.KEY_DIVEPICTURE));
c.moveToLast();
noOfRows = Integer.parseInt(c.getString(c.getColumnIndex(diveDataBase.KEY__DIVENUMBER)));
holder.dive_no.setText(diveNumber+"/"+noOfRows);
//set image here once taken form external string path, and resized bitmap conversion
getImageAsynch = (getBitmapImage) new getBitmapImage(v).execute(diveImagePath);
这是一个异步内部类,在这个类中,图像从路径获取到ext目录,调整大小,然后填充视图持有者
类getBitmapImage扩展了异步任务{
private View view;
private ViewHolder holderB;
public getBitmapImage(View v) {
// TODO Auto-generated constructor stub, takes ViewGroup as arg
//ViewGroup parent;
view=v;
holderB=new ViewHolder();
}
@Override
protected Bitmap doInBackground(String... imagePath) {
/*
* get image path and decode to bitmap
* First must make sure image loaded from DB base 64 is not loaed into memory
* at full size ie 1028 * 800 pixels
* Instead we use BitMapOptions object methods inJustDecodeBounds
* to stop autoloading of image,
* then we scale down the image for loading into memory using
* BitMapOPtiontions.inSampleSize method
* This will significantly reduce memory usage and time req to load images into list view
*
* first check if user wants to preview images (boolean displayImagesUserChoice), if not return null,
* this value is passed from dialog propmt in ViewListOfDives
* and passed to ItemAdpter constructor
*/
if(displayImagesUserChoice){
if(!isCancelled()){
String diveImagePath = imagePath[0];
File imagePathFile = new File(diveImagePath);
try {
final int IMAGE_MAX_SIZE = 3000;
FileInputStream streamIn = new FileInputStream(imagePathFile);
// Decode image size and setInJBounds = true to avoid auto memory allocation for large image
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(streamIn, null, o);
streamIn.close();
-----allot of resizing image code here have removed for an easier read---
streamIn.close();
b.recycle();
System.gc();
} else {
bitmap = BitmapFactory.decodeStream(streamIn);
resizedImage = reSizeImage(bitmap);
streamIn.close();
noOfImagesloaded++;
System.gc();
}
//resizedImage = reSizeImage(bitmap);
}catch(IOException exe){
exe.printStackTrace();
}catch(OutOfMemoryError exc){
exc.printStackTrace();
//Toast.makeText(this, "Something went wrong! Try again...", Toast.LENGTH_SHORT).show();
}catch(NullPointerException nullpoint){
nullpoint.printStackTrace();
//end try catch
}
}//end if anstch class notCancelled from the onCakPressed method of viewListOfDives
else if(isCancelled()){
Log.d("ItemAdpter Aycnh", "Do in background cancelled");
}
Log.d("ItemApdter Aycnh", "No of Images loaded = "+ noOfImagesloaded);
//return bitmap;
}//end if displayImagesUserChoice=true, if not true resizedIMage is returned as null to onPostExecute
return resizedImage;
}//end do in background
@Override
protected void onPostExecute(Bitmap bitmap) {
//intilise holder to listview.xml ImageView
holderB.displayImage = (ImageView) view.findViewById(R.id.iv_list_image); //image view
//ImageView displayImage = (ImageView) view.findViewById(R.id.iv_list_image);
if(bitmap!=null ){
//set image using holder static object
holderB.displayImage.setBackground(null);
holderB.displayImage.setImageBitmap(resizedImage);
}else{
//set default image using static holder object
holderB.displayImage.setBackgroundResource(R.drawable.camera4);
}
}//end onPOstExecute
}//end getBitmap asynch
包含文本视图和分级栏的视图保持器返回的速度比从sd卡异步获取的图像快,因此在使用图像填充视图时会有延迟。异步删除图像加载会消除问题,但在加载所有图像之前,GUI没有响应,因此别无选择,只能使用异步任务