Web からデータを取得し、それを 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;
}
}
}
バツ
その混乱を修正して、上/下にスクロールした後でも写真を元の位置に保つにはどうすればよいですか?