Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将图片从URL加载到ListView中的ImageView_Android_Image_Listview_Custom Adapter - Fatal编程技术网

Android 将图片从URL加载到ListView中的ImageView

Android 将图片从URL加载到ListView中的ImageView,android,image,listview,custom-adapter,Android,Image,Listview,Custom Adapter,我有一个带有自定义适配器的android Listview。listview中的每个项目都在imageview中携带一张新闻图片,需要使用其URL加载图像。我的代码如下。当我运行它时,我只得到前几个项目的图片。当我向下滚动到其他项目时,我会看到每个图像视图中的进度条一直旋转。我可以在日志中看到一些图片无法加载,但我不理解为什么这会影响所有图片,因为我正在处理异常。如果我能知道我犯了什么错误,我将不胜感激 下面是我的自定义适配器 public class CustomAdapter extends

我有一个带有自定义适配器的android Listview。listview中的每个项目都在imageview中携带一张新闻图片,需要使用其URL加载图像。我的代码如下。当我运行它时,我只得到前几个项目的图片。当我向下滚动到其他项目时,我会看到每个图像视图中的进度条一直旋转。我可以在日志中看到一些图片无法加载,但我不理解为什么这会影响所有图片,因为我正在处理异常。如果我能知道我犯了什么错误,我将不胜感激

下面是我的自定义适配器

public class CustomAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private ArrayList<CustomObject> objects;
    private Activity activity;
    private ArrayList<Bitmap> newsImageList;

    private class ViewHolder {
        TextView titleTextView;
        TextView dateTextView;
        TextView bodyTextView;
        ImageView logoView;
        ImageView newsImageView;
        ProgressBar progressBar;
    }


    public CustomAdapter(Context context, ArrayList<CustomObject> objects, Activity activity) {
       // super(context, R.layout.news_item, objects);

        inflater = LayoutInflater.from(context);
        this.objects = objects;
        this.activity= activity;
        this.newsImageList= new ArrayList<Bitmap>();
        for(int i=0; i<objects.size(); i++)
            newsImageList.add(null);

    }

    @Override
    public int getCount() {
        return objects.size();
    }

    @Override
    public CustomObject getItem(int position) {
        return objects.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        System.out.println("IN GET VIEW");

        ViewHolder holder = null;
        if(convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.news_item, null);

            holder.titleTextView = (TextView) convertView.findViewById(R.id.txtTitle);
            holder.dateTextView = (TextView) convertView.findViewById(R.id.txtDate);
            holder.bodyTextView = (TextView) convertView.findViewById(R.id.txtBody);
            holder.logoView= (ImageView)  convertView.findViewById(R.id.source_imageView);
            holder.newsImageView= (ImageView) convertView.findViewById(R.id.news_imageView);
            holder.progressBar= (ProgressBar) convertView.findViewById(R.id.img_progress_bar);


            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.titleTextView.setText(objects.get(position).getTitle());
        holder.dateTextView.setText(objects.get(position).getDate());
        holder.bodyTextView.setText(objects.get(position).getBody());

        //for the source logo
        String uri = "@drawable/"+objects.get(position).getSource_logo()+"_logo";
        int imageResource = activity.getResources().getIdentifier(uri, null, activity.getPackageName());
        Drawable res =activity.getResources().getDrawable(imageResource);
        holder.logoView.setImageDrawable(res);





        //the news image
        String newsImageURL = objects.get(position).getNewsImageURL();

        if(newsImageURL!=null) {
            // show The Image in a ImageView

            //already loaded
            if(newsImageList.get(position)!=null) {
                holder.progressBar.setVisibility(View.INVISIBLE);
                holder.newsImageView.setImageBitmap(newsImageList.get(position));
            }
            else
                new DownloadImageTask(holder.newsImageView, holder, position)
                    .execute(newsImageURL);
        }
        else
        {
            System.out.println("NO IMAGE");
            //adjusting the view
            holder.progressBar.setVisibility(View.INVISIBLE);
            holder.newsImageView.setVisibility(View.GONE);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.bodyTextView.getLayoutParams();
            params.addRule(RelativeLayout.BELOW, R.id.txtDate);
        }


        return convertView;
    }

    //subclass
    class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
        ImageView bmImage;
        boolean imageLoadingFailure=false;//default is false
        ViewHolder holder;
        int index;

        public DownloadImageTask(ImageView bmImage,ViewHolder holder, int index) {
            this.bmImage = bmImage;
            this.holder= holder;
            this.index=index;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            holder.newsImageView.setImageResource(android.R.color.transparent);//clear the news image view
            holder.progressBar.setVisibility(View.VISIBLE);//show the progress bar
            holder.newsImageView.setVisibility(View.VISIBLE);//show the image view

        }

        protected Bitmap doInBackground(String... urls) {
            return downloadBitmap(urls[0]);
        }

        protected void onPostExecute(Bitmap result) {

            if(imageLoadingFailure)
            {
                //adjusting the view
                System.out.println("IMAGE LOADING FAILED!");

                /*
                holder.progressBar.setVisibility(View.INVISIBLE);
                holder.newsImageView.setVisibility(View.GONE);
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.bodyTextView.getLayoutParams();
                params.addRule(RelativeLayout.BELOW, R.id.txtDate);
                */
            }
            else {
                holder.progressBar.setVisibility(View.INVISIBLE);
                bmImage.setImageBitmap(result);
                newsImageList.set(index, result);
            }



        }

        //code source: http://stacktips.com/tutorials/android/loading-image-asynchronously-in-android-listview
        private Bitmap downloadBitmap(String url) {
            HttpURLConnection urlConnection = null;
            try {
                URL uri = new URL(url);
                urlConnection = (HttpURLConnection) uri.openConnection();
                int statusCode = urlConnection.getResponseCode();
            /*if (statusCode != HttpStatus.SC_OK) {
                return null;
            }*/

                InputStream inputStream = urlConnection.getInputStream();
                if (inputStream != null) {
                    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                    return bitmap;
                }
            } catch (Exception e) {
                urlConnection.disconnect();
                imageLoadingFailure=true;
                Log.w("ImageDownloader", "Error downloading image from " + url);
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
            return null;
        }



    }





}
公共类CustomAdapter扩展了BaseAdapter{
私人充气机;
私有ArrayList对象;
私人活动;
私有ArrayList newsImageList;
私有类视窗持有者{
text视图titleTextView;
文本视图日期文本视图;
TextView bodyTextView;
图像视图logoView;
ImageView新闻ImageView;
ProgressBar ProgressBar;
}
公共CustomAdapter(上下文上下文、ArrayList对象、活动){
//super(上下文、R.layout.news\u项目、对象);
充气器=充气器。从(上下文);
this.objects=对象;
这个。活动=活动;
this.newsImageList=newArrayList();
对于(int i=0;i使用毕加索

compile 'com.squareup.picasso:picasso:2.5.2'
代码


尝试使用Glide从URL加载图像使用Glide…有很多。大多数人知道如何处理视图回收(
ListView
RecyclerView
,等等)。我强烈建议您使用这些库中的一个。使用毕加索,这是实现图像加载库的最简单和最轻的方法。毕加索谢谢。我很快就会尝试。我需要在我的项目中添加库吗?在项目中添加gradle以上的库
compile 'com.squareup.picasso:picasso:2.5.2'
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);