我在我的應用中使用了Parse,爲了加載我的'profile'圖像,我需要檢索所謂的Parsefile。當Parsefile被下載時,它使用回調來通知它何時完成。現在,這通常是一種很好的做事方式,但在使用Listview並使用Asynctask下載圖像時遇到了問題。在回調中異步加載圖像
的問題如下:
在getView
方法我的ListView適配器,我創建的AsyncTask並執行它,這樣的AsyncTask開始retrieveProfileImage(callBack)
功能。在我的回調中,我只需在UI線程上啓動一個Runnable,以使用新的(檢索的圖像)更新View中的ImageView。不過,看起來問題是,只要我啓動AsyncTask,視圖就會返回。所以我無法將其他圖像設置爲正確的行。我希望我的代碼更清楚地表明我的問題。
的ListAdapter:
public class FriendListAdapter extends ArrayAdapter<Profile> {
private int resource;
private Context context;
private List<Profile> friends;
private Profile fProfile;
private Bitmap profileImageBitmap;
private ProgressBar friendImageProgressBar;
//ui
private ImageView friendImage;
public FriendListAdapter(Context context, int resource,
List<Profile> objects) {
super(context, resource, objects);
this.context = context;
this.resource = resource;
this.friends = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView friendName = null;
friendImage = null;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.friendslist_row, null);
friendName = (TextView) rowView.findViewById(R.id.fName);
friendImage = (ImageView) rowView
.findViewById(R.id.fImage);
friendImageProgressBar = (ProgressBar) rowView.findViewById(R.id.friendImageProgressBar);
} else {
friendName = (TextView) convertView.findViewById(R.id.fName);
friendImage = (ImageView) convertView.findViewById(R.id.fImage);
friendImageProgressBar = (ProgressBar) convertView.findViewById(R.id.friendImageProgressBar);
}
fProfile = friends.get(position);
DownloadProfileImage dImg = new DownloadProfileImage();
dImg.execute();
friendName.setText(fProfile.getName());
return rowView;
}
private class DownloadProfileImage extends AsyncTask<Void, Integer, String> {
@Override
protected String doInBackground(Void... arg0) {
Log.d("logpp", "Starting download image for " + fProfile.getName());
fProfile.retrieveProfileImage(new ProfileImageCallback());
return null;
}
}
private class ProfileImageCallback extends GetDataCallback {
@Override
public void done(byte[] bytearray, ParseException e) {
if (e == null) {
Log.d("logpp", "Done downloading image for " + fProfile.getName() + ". Setting bitmap to:" +
" " + friendImage.getId());
profileImageBitmap = BitmapManager
.getBitmapFromByteArray(bytearray);
((Activity) context).runOnUiThread(new UpdateUi());
}
}
}
private class UpdateUi implements Runnable {
@Override
public void run() {
friendImage.setImageBitmap(profileImageBitmap);
friendImage.setVisibility(View.VISIBLE);
friendImageProgressBar.setVisibility(View.INVISIBLE);
}
}
}
的retrieveProfileImage方法:
public void retrieveProfileImage(GetDataCallback callBack) {
this.image.getDataInBackground(callBack);
}
我希望有人能幫助我這一個。
問候,
添
使用延遲加載與像通用圖像裝載機和畢加索庫。使用UIL http://stackoverflow.com/questions/16789676/caching-images-and-displaying – Raghunandan
@Raghunandan事情是,我不能使用網址。我沒有圖像網址,我可以通過使用回調來檢索圖像(位圖)。解析庫限制了我的使用。 –