在后台下载图片显示问题
Downloading images in the background issue displaying
我目前正在做的是允许用户查找曲目,它会显示歌曲名称和艺术家,然后开始在后台下载图像,以便用户快速看到结果。我目前所做的是获取前 10 个查询并显示这些查询并下载这些图像,然后当用户单击显示更多时它将再下载 10 个,依此类推,但是加载完成后实际显示的唯一图像是第一个.其余图像下载,但图像视图不会填充,除非我滚动离开它们然后再返回它们。我不明白为什么,但这是我的代码。
我的自定义列表适配器代码:
public class SearchSongAdapter extends BaseAdapter {
ArrayList<ArrayList<String>> track_info;
private static LayoutInflater inflater=null;
String token;
ArrayList<ImageView> imageViews;
ArrayList<Bitmap> imageBitMaps;
DownloadImageTask downloadImageTask;
int downloadsCounter = 0;
public SearchSongAdapter(Context context, ArrayList<ArrayList<String>> track_info, String token)
{
imageViews = new ArrayList<ImageView>();
imageBitMaps = new ArrayList<Bitmap>();
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.track_info = track_info;
this.token = token;
//start the downloads
if(track_info.size() > 0) {
downloadImageTask = new DownloadImageTask();
downloadImageTask.execute("https://api.spotify.com/v1/tracks/" + track_info.get(0).get(1).replace("spotify:track:", ""), String.valueOf(downloadsCounter));
}
}
@Override
public int getCount() {
return this.track_info.size();
}
@Override
public Object getItem(int position) {
return track_info.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView songNameTextView, artistNameTextView;
ImageView trackIconImageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
Holder holder = new Holder();
rowView = inflater.inflate(R.layout.song_list_items, null);
holder.songNameTextView = (TextView) rowView.findViewById(R.id.songName);
holder.artistNameTextView = (TextView) rowView.findViewById(R.id.artistNameTextView);
holder.trackIconImageView = (ImageView) rowView.findViewById(R.id.trackIconImageView);
holder.songNameTextView.setText(track_info.get(position).get(0));
holder.artistNameTextView.setText(track_info.get(position).get(2));
imageViews.add(position, holder.trackIconImageView);
if(position < imageBitMaps.size()) {
holder.trackIconImageView.setImageBitmap(imageBitMaps.get(position));
}
return rowView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
int position;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
HttpResponse response = null;
HttpClient httpClient = new DefaultHttpClient();
String albumpicture;
Bitmap mIcon11 = null;
position = Integer.valueOf(urls[1]);
HttpGet httpPost = new HttpGet(urls[0]);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Accept", "application/json"));
params.add(new BasicNameValuePair("Authorization Bearer ", token));
try {
response = httpClient.execute(httpPost);
// writing response to log
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity()));
albumpicture = jsonObject.getJSONObject("album").getJSONArray("images").getJSONObject(0).getString("url");
InputStream in = new java.net.URL(albumpicture).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
mIcon11 = getResizedBitmap(mIcon11, 50, 50);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageViews.get(position).setImageBitmap(bitmap);
imageBitMaps.add(position, bitmap);
if(position < track_info.size() - 1) {
downloadImageTask = new DownloadImageTask();
downloadImageTask.execute("https://api.spotify.com/v1/tracks/" + track_info.get(downloadsCounter).get(1).replace("spotify:track:", ""), String.valueOf(downloadsCounter));
}
downloadsCounter++;
}
}
DownloadImageTask 是我下载图像的地方,我首先在构造函数中调用它。
总结:
第一个 imageView 加载并填充,但其他的不填充,除非我从那里滚动(向下滚动)然后返回。
我们有很棒的开源库来处理图像下载和缓存内容 - Picasso Square。
我们应该始终尝试使用现有的和经过验证的解决方案,而不是试图重新发明轮子。
@Rockyfish,
您可以减少自己加载 Images
和管理网络调用的麻烦。
你的加载图片逻辑可以用这样一行代码代替。
Picasso.with(getApplicationContext()).load(image_url).into(holder.trackIconImageView);
where,
getApplicationContext() ===> is context which can be replaced with context you you are assigning to the `adapter`
image_url =================> is the url to the imgae,
从保存在某个 url 的图像加载 ImgaeView 的过程由 Picasso
完成,因为您知道他是一位伟大的画家所以让他来完成这项工作。
添加 Picasso
库,将以下代码添加到 build.gradle
文件中的依赖项块,如下所示
compile 'com.squareup.picasso:picasso:2.5.2'
你可以使用 picasso
我目前正在做的是允许用户查找曲目,它会显示歌曲名称和艺术家,然后开始在后台下载图像,以便用户快速看到结果。我目前所做的是获取前 10 个查询并显示这些查询并下载这些图像,然后当用户单击显示更多时它将再下载 10 个,依此类推,但是加载完成后实际显示的唯一图像是第一个.其余图像下载,但图像视图不会填充,除非我滚动离开它们然后再返回它们。我不明白为什么,但这是我的代码。
我的自定义列表适配器代码:
public class SearchSongAdapter extends BaseAdapter {
ArrayList<ArrayList<String>> track_info;
private static LayoutInflater inflater=null;
String token;
ArrayList<ImageView> imageViews;
ArrayList<Bitmap> imageBitMaps;
DownloadImageTask downloadImageTask;
int downloadsCounter = 0;
public SearchSongAdapter(Context context, ArrayList<ArrayList<String>> track_info, String token)
{
imageViews = new ArrayList<ImageView>();
imageBitMaps = new ArrayList<Bitmap>();
inflater = (LayoutInflater)context.
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.track_info = track_info;
this.token = token;
//start the downloads
if(track_info.size() > 0) {
downloadImageTask = new DownloadImageTask();
downloadImageTask.execute("https://api.spotify.com/v1/tracks/" + track_info.get(0).get(1).replace("spotify:track:", ""), String.valueOf(downloadsCounter));
}
}
@Override
public int getCount() {
return this.track_info.size();
}
@Override
public Object getItem(int position) {
return track_info.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public class Holder
{
TextView songNameTextView, artistNameTextView;
ImageView trackIconImageView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView;
Holder holder = new Holder();
rowView = inflater.inflate(R.layout.song_list_items, null);
holder.songNameTextView = (TextView) rowView.findViewById(R.id.songName);
holder.artistNameTextView = (TextView) rowView.findViewById(R.id.artistNameTextView);
holder.trackIconImageView = (ImageView) rowView.findViewById(R.id.trackIconImageView);
holder.songNameTextView.setText(track_info.get(position).get(0));
holder.artistNameTextView.setText(track_info.get(position).get(2));
imageViews.add(position, holder.trackIconImageView);
if(position < imageBitMaps.size()) {
holder.trackIconImageView.setImageBitmap(imageBitMaps.get(position));
}
return rowView;
}
private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
int position;
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
HttpResponse response = null;
HttpClient httpClient = new DefaultHttpClient();
String albumpicture;
Bitmap mIcon11 = null;
position = Integer.valueOf(urls[1]);
HttpGet httpPost = new HttpGet(urls[0]);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("Accept", "application/json"));
params.add(new BasicNameValuePair("Authorization Bearer ", token));
try {
response = httpClient.execute(httpPost);
// writing response to log
} catch (IOException e) {
e.printStackTrace();
}
try {
try {
JSONObject jsonObject = new JSONObject(EntityUtils.toString(response.getEntity()));
albumpicture = jsonObject.getJSONObject("album").getJSONArray("images").getJSONObject(0).getString("url");
InputStream in = new java.net.URL(albumpicture).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
mIcon11 = getResizedBitmap(mIcon11, 50, 50);
} catch (JSONException e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageViews.get(position).setImageBitmap(bitmap);
imageBitMaps.add(position, bitmap);
if(position < track_info.size() - 1) {
downloadImageTask = new DownloadImageTask();
downloadImageTask.execute("https://api.spotify.com/v1/tracks/" + track_info.get(downloadsCounter).get(1).replace("spotify:track:", ""), String.valueOf(downloadsCounter));
}
downloadsCounter++;
}
}
DownloadImageTask 是我下载图像的地方,我首先在构造函数中调用它。
总结:
第一个 imageView 加载并填充,但其他的不填充,除非我从那里滚动(向下滚动)然后返回。
我们有很棒的开源库来处理图像下载和缓存内容 - Picasso Square。
我们应该始终尝试使用现有的和经过验证的解决方案,而不是试图重新发明轮子。
@Rockyfish,
您可以减少自己加载 Images
和管理网络调用的麻烦。
你的加载图片逻辑可以用这样一行代码代替。
Picasso.with(getApplicationContext()).load(image_url).into(holder.trackIconImageView);
where,
getApplicationContext() ===> is context which can be replaced with context you you are assigning to the `adapter`
image_url =================> is the url to the imgae,
从保存在某个 url 的图像加载 ImgaeView 的过程由 Picasso
完成,因为您知道他是一位伟大的画家所以让他来完成这项工作。
添加 Picasso
库,将以下代码添加到 build.gradle
文件中的依赖项块,如下所示
compile 'com.squareup.picasso:picasso:2.5.2'
你可以使用 picasso