Android Listview从url加载imageview
我正在从url加载一个图像并将其输入我的listview。问题是每次我滚动listview时,imageview都会更改其图像 我使用以下代码:从url加载图像:Android Listview从url加载imageview,android,listview,Android,Listview,我正在从url加载一个图像并将其输入我的listview。问题是每次我滚动listview时,imageview都会更改其图像 我使用以下代码:从url加载图像: public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> { private String url; private ImageView imageView; public ImageLoadTask(String url,
public class ImageLoadTask extends AsyncTask<Void, Void, Bitmap> {
private String url;
private ImageView imageView;
public ImageLoadTask(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
URL urlConnection = new URL(url);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
imageView.setImageBitmap(result);
}
}
在我的主要活动中:
public void get_data(String data){
try {
JSONArray data_array=new JSONArray(data);
for (int i = 0 ; i < data_array.length() ; i++){
JSONObject obj=new JSONObject(data_array.get(i).toString());
Countries add=new Countries();
add.title = obj.getString("title");
add.thumb = obj.getString("artwork_url");
countries.add(add);
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
public void get_数据(字符串数据){
试一试{
JSONArray数据_数组=新的JSONArray(数据);
对于(int i=0;i
您应该使用像这样的图像加载库。它使从URL加载图像变得非常简单,并且还可以进行缓存 您应该使用像这样的图像加载库。它使从URL加载图像变得非常简单,并且还可以进行缓存 尝试使用。它使用起来很简单
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
毕加索有自动内存和磁盘缓存。它在列表视图中非常重要
在哪里可以获得变量名上下文
获取上下文:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context = parent.getContext();
[...]
}
尝试使用。它使用起来很简单
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
毕加索有自动内存和磁盘缓存。它在列表视图中非常重要
在哪里可以获得变量名上下文
获取上下文:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Context context = parent.getContext();
[...]
}
我同意现有的两个答案,避免任何头痛并使您的生活更轻松的最佳方法是使用当前可用的图像库之一:和
与毕加索相比,Glide同样易于使用。我看过一段视频,其中使用列表视图时,图像加载到毕加索上的时间比使用Glide要长,但这可能是由于配置设置。Glide还可以使用GIF,这些GIF对您可能重要,也可能不重要
下面是一个使用Glide的示例:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("https://lh6.ggpht.com/9SZhHdv4URtBzRmXpnWxZcYhkgTQurFuuQ8OR7WZ3R7fyTmha77dYkVvcuqMu3DLvMQ=w300").into(imageView);
希望这有帮助 我同意现有的两个答案,避免任何头痛并使您的生活更轻松的最佳方法是使用当前可用的图像库之一:和
与毕加索相比,Glide同样易于使用。我看过一段视频,其中使用列表视图时,图像加载到毕加索上的时间比使用Glide要长,但这可能是由于配置设置。Glide还可以使用GIF,这些GIF对您可能重要,也可能不重要
下面是一个使用Glide的示例:
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
Glide.with(this).load("https://lh6.ggpht.com/9SZhHdv4URtBzRmXpnWxZcYhkgTQurFuuQ8OR7WZ3R7fyTmha77dYkVvcuqMu3DLvMQ=w300").into(imageView);
希望这有帮助 您需要尝试下载并缓存ListView
的图像库。这可以提高性能,并轻松地将图像加载到ImageView
中
和是将ImageView
中的图像加载到ListView
的最佳示例。您需要尝试下载图像并缓存ListView
的库。这可以提高性能,并轻松地将图像加载到ImageView
中
和是将ImageView
中的图像加载到ListView
的最佳示例。在哪里可以获取变量名“context”?@tokis Edited my aswer。在哪里可以获取变量名“context”?@tokis Edited my aswer。