此自定义适配器加载的图像放置在错误的位置,即正确的电影横幅未放置在正确的列表视图项中。并在一段时间内不断变化。这是我的带有 ASYNCTASK 的自定义适配器,它从 URL 加载图像
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androlizer.yify.torrent.R;
import androlizer.yify.torrents.models.UpcomingMovieListModel;
public class UpcomingMoviesCustomAdapter extends ArrayAdapter<UpcomingMovieListModel> {
Context context;
public UpcomingMoviesCustomAdapter(
Context context, int resource, List<UpcomingMovieListModel> objects) {
super(context, resource, objects);
this.context = context;
}
static class ViewHolder
{
TextView movieTitle_textView;
TextView uploader_textView;
TextView date_textView;
ImageView movie_icon_imageView;
ImageView imdb_url_imageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
// getting data
final UpcomingMovieListModel movie = getItem(position);
if (convertView == null)
{
convertView = View.inflate(context, R.layout.movie_upcoming_row, null);
holder = new ViewHolder();
holder.movieTitle_textView = (TextView) convertView.findViewById(R.id.movie_upcoming_movie_title);
holder.uploader_textView = (TextView) convertView.findViewById(R.id.movie_upcoming_uploader);
holder.date_textView = (TextView) convertView.findViewById(R.id.movie_upcoming_date);
holder.imdb_url_imageView = (ImageView)convertView.findViewById(R.id.movie_upcoming_imageView_imdblink);
holder.movie_icon_imageView = (ImageView)convertView.findViewById(R.id.movie_upcoming_movie_image_view);
convertView.setTag(holder);
}
else
{
holder = (ViewHolder)convertView.getTag();
}
if (movie != null)
{
holder.movieTitle_textView.setText(movie.getM_title());
holder.uploader_textView.setText(movie.getUploader());
SimpleDateFormat origFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//Store it as a date object
Date date = null;
try {
date = origFormat.parse(movie.getDate_added());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//Output it as a string that uses the new format
SimpleDateFormat newFormat= new SimpleDateFormat("MMMMMMMMM dd, yyyy 'at' hh:mm a");
String desiredDateFormat = newFormat.format(date);
holder.date_textView.setText(desiredDateFormat);
holder.imdb_url_imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(movie.getImdb_url())));
}
});
}
new ImageLoader().execute(convertView.g, movie.getM_cover());
return convertView;
}
public class ImageLoader extends AsyncTask<Object, String, Bitmap> {
private View view;
private Bitmap bitmap = null;
@Override
protected Bitmap doInBackground(Object... parameters) {
// Get the passed arguments here
view = (View) parameters[0];
String uri = (String)parameters[1];
// Create bitmap from passed in Uri here
// ...
try {
URL req = new URL(uri);
bitmap = BitmapFactory.decodeStream(req.openConnection()
.getInputStream());
} catch (Exception e) {
// TODO: handle exception
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && view != null) {
ImageView splash = (ImageView) view.findViewById(R.id.movie_upcoming_movie_image_view);
splash.setImageBitmap(bitmap);
}
}
}
}