我从网上获取一些数据,然后使用我的 ArrayAdapter 将其传递到 listView。
每行都有自己的 ID。和它自己的形象..这也来自网络。
每次返回一行之前,我都会启动 AsyncTask ImageDownloader
。
但是,当我在我现在面临的图像加载之前滚动列表视图时,它会创建混乱和随机图像加载到错误行的 ImageViews 中。
这就是我在我的适配器中所做的:
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MobileArrayAdapter extends ArrayAdapter<List<item>> {
private final Context context;
private final List<item> markers;
private final static String[] a={"s"};
ImageView image;
ProgressBar loader;
public MobileArrayAdapter(Context context, List<item> markers) {
super(context, R.layout.list_item);
this.context = context;
this.markers = markers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.list_item, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
loader = (ProgressBar) rowView.findViewById(R.id.loader);
image = (ImageView) rowView.findViewById(R.id.itemimgae);
TextView views = (TextView) rowView.findViewById(R.id.views);
TextView likes=(TextView) rowView.findViewById(R.id.likes);
TextView upvote=(TextView) rowView.findViewById(R.id.upvote);
TextView downvote=(TextView) rowView.findViewById(R.id.downvote);
TextView desc=(TextView) rowView.findViewById(R.id.desc);
TextView pub =(TextView) rowView.findViewById(R.id.pub);
TextView idnum =(TextView) rowView.findViewById(R.id.idnum);
title.setText(" "+markers.get(position).getTitle());
views.setText(markers.get(position).getView()+"");
likes.setText(markers.get(position).getLike()+"");
upvote.setText(markers.get(position).getUpvote()+"");
downvote.setText(markers.get(position).getDownvote()+"");
desc.setText(markers.get(position).getDesc());
pub.setText(markers.get(position).getPub()+"");
idnum.setText(markers.get(position).getId()+"");
new ImageDownloader()
.execute("http://www.myserver.page.php?h=400&img=upload/id"
+ markers.get(position).getId() + ".jpg");
return rowView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return (this.markers != null) ? this.markers.size() : 0;
}
private class ImageDownloader extends AsyncTask<String, String, Bitmap> {
protected void onPreExecute(String res) {
Log.i("Async-Example", "onPreExecute Called");
}
protected Bitmap doInBackground(String... param) {
// TODO Auto-generated method stub
return downloadBitmap(param[0]);
}
protected void onPostExecute(Bitmap result) {
Log.i("Async-Example", "onPostExecute Called");
image.setImageBitmap(result);
loader.setVisibility(View.INVISIBLE);
}
private Bitmap downloadBitmap(String url) {
// initilize the default HTTP client object
final DefaultHttpClient client = new DefaultHttpClient();
// forming a HttoGet request
final HttpGet getRequest = new HttpGet(url);
try {
HttpResponse response = client.execute(getRequest);
// check 200 OK for success
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
Log.w("ImageDownloader", "Error " + statusCode
+ " while retrieving bitmap from " + url);
return null;
}
final HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = null;
try {
// getting contents from the stream
inputStream = entity.getContent();
// decoding stream data back into image Bitmap that
// android understands
final Bitmap bitmap = BitmapFactory
.decodeStream(inputStream);
return bitmap;
} finally {
if (inputStream != null) {
inputStream.close();
}
entity.consumeContent();
}
}
} catch (Exception e) {
// You Could provide a more explicit error message for
// IOException
getRequest.abort();
Log.e("ImageDownloader", "Something went wrong while"
+ " retrieving bitmap from " + url + e.toString());
}
return null;
}
}
}
X
我怎样才能解决这个问题并让我的照片即使在它们上方/下方滚动后也保持在原来的位置?