0
I m trying to make a lisView with two textView and an imageView (that come as a url Sting) on each item at the list but, the list is not scrolling as good as I want, because its taking too long to load the image url. Im using an AsyncTask class for loading the the image but still it dosen
t看起來不錯。 這裏是我的代碼以int一個ArrayAdapter類:使用分頁將圖像url加載到項目ListView中Android
public class MySimpleArrayAdapter extends ArrayAdapter<Movie> {
final private Context context;
final private Movie[] movies;
ImageView movieIcon;
TextView name, description;
Bitmap bitmap;
public MySimpleArrayAdapter(Context context, Movie[] movies) {
super(context,R.layout.item_in_movielist, movies);
this.context = context;
this.movies = movies;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.item_in_movielist, parent, false);
name = (TextView) rowView.findViewById(R.id.tvMovieName);
description = (TextView) rowView.findViewById(R.id.tvMovieDescription);
movieIcon = (ImageView) rowView.findViewById(R.id.ivMovieIcon);
GetImageAsync getImageAsync = new GetImageAsync();
getImageAsync.imageView = movieIcon;
name.setText(movies[position].getMovieName());
description.setText(movies[position].getMovieDescription());
getImageAsync.execute(position);
return rowView;
}
public class GetImageAsync extends AsyncTask<Integer, Void, Bitmap> {
public ImageView imageView;
@Override
protected void onPostExecute(Bitmap bitmap1) {
imageView.setImageBitmap(bitmap1);
}
@Override
protected Bitmap doInBackground(Integer... params) {
URL url = null;
try {
url = new URL(movies[params[0]].getMovieImgURL());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
return BitmapFactory.decodeStream(input);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
}
我瞭解,這是不的方式做到這一點,我在找改變我的代碼爲「尋呼」和我想正確地做。 任何提示我能做些什麼?
P.S 如果您可以告訴我如何將Paging添加到此代碼中,那將非常棒。
謝謝!
感謝您的快速回放,我應該使用它來代替AsynTask類還是在它內部? –
是使用這個而不是AsynTask類 –