0

Hi my app is about parsing xml file and then load the image from url and also the text and display it in a list view, but whenever am loading the image from url and while scrolling the list view little down it can't load the image properly and it force closes the app. Now how to overcome the situation.Please help here is my code for downloading the image from url and also the text.

public class NewsRowAdapter extends ArrayAdapter<Item> 
{
    LoadingImage loadingImage;
    Bitmap bitmap = null;
    private Activity activity;
    private List<Item> items;
    private Item objBean;
    private int row;
    /*private DisplayImageOptions options;
    ImageLoader imageLoader;*/

    public NewsRowAdapter(Activity act, int resource, List<Item> arrayList) 
    {
        super(act, resource, arrayList);
        this.activity = act;
        this.row = resource;
        this.items = arrayList;

        /*options = new DisplayImageOptions.Builder().showStubImage(R.drawable.blank).showImageForEmptyUrl(R.drawable.blank).cacheInMemory().cacheOnDisc().build();
        imageLoader = ImageLoader.getInstance();*/


    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent)
    {
        View view = convertView;
        ViewHolder holder;
        if (view == null) 
        {
            LayoutInflater inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(row, null);

            holder = new ViewHolder();
            view.setTag(holder);
        } else 
        {
            holder = (ViewHolder) view.getTag();
        }

        if ((items == null) || ((position + 1) > items.size()))
            return view;

        objBean = items.get(position);

        holder.tvTitle = (TextView) view.findViewById(R.id.tvtitle);
        holder.tvDesc = (TextView) view.findViewById(R.id.tvdesc);
        holder.tvDate = (TextView) view.findViewById(R.id.tvdate);
        holder.imgView = (ImageView) view.findViewById(R.id.image);
        holder.pbar = (ProgressBar) view.findViewById(R.id.pbar);

        if (holder.tvTitle != null && null != objBean.getTitle() && objBean.getTitle().trim().length() > 0) 
        {
            holder.tvTitle.setText(Html.fromHtml(objBean.getTitle()));
        }
        if (holder.tvDesc != null && null != objBean.getDesc()
                && objBean.getDesc().trim().length() > 0) {
            holder.tvDesc.setText(Html.fromHtml(objBean.getDesc()));
        }
        if (holder.tvDate != null && null != objBean.getPubdate() && objBean.getPubdate().trim().length() > 0)
        {
            holder.tvDate.setText(Html.fromHtml(objBean.getPubdate()));
        }
        if (holder.imgView != null) 
        {
            if (null != objBean.getLink() && objBean.getLink().trim().length() > 0) 
            {
                final ProgressBar pbar = holder.pbar;
                pbar.setVisibility(View.INVISIBLE);
                //---------CHANGES MADE FOR LOADING IMAGE----------//
                Log.d("IMAGE NULL----------", objBean.getLink());


                try 
                {

                    URL linkurl = new URL(objBean.getLink());

                    bitmap = BitmapFactory.decodeStream(linkurl.openConnection().getInputStream());

                    holder.imgView.setImageBitmap(bitmap);

                } catch (MalformedURLException e) 
                {
                    e.printStackTrace();

                } catch (IOException e) 
                {
                    e.printStackTrace();
                }


                /*try 
                {
                    loadBitmap(objBean.getLink());

                } catch (IOException e) 
                {
                    e.printStackTrace();
                }
                */

                /*imageLoader.init(ImageLoaderConfiguration.createDefault(activity));
                imageLoader.displayImage(objBean.getLink(), holder.imgView, options, new ImageLoadingListener()
                {
                            @Override
                            public void onLoadingComplete() 
                            {
                                pbar.setVisibility(View.INVISIBLE);

                            }

                            @Override
                            public void onLoadingFailed() 
                            {
                                pbar.setVisibility(View.INVISIBLE);
                            }

                            @Override
                            public void onLoadingStarted()
                            {
                                pbar.setVisibility(View.VISIBLE);

                            }
                });*/

            } else 
            {
                holder.imgView.setImageResource(R.drawable.ic_launcher);
            }
        }

        return view;
    }





    public class ViewHolder
    {

        public TextView tvTitle, tvDesc, tvDate;
        private ImageView imgView;
        private ProgressBar pbar;

    }

} 
ayansinha
  • 85
  • 1
  • 12

2 Answers2

1

Use AsyncTask for downloading the images. Check this: http://android-developers.blogspot.in/2010/07/multithreading-for-performance.html

user1744952
  • 508
  • 1
  • 3
  • 15
  • Yes it will work. It will do the downloading process in a thread (Not UI Thread) and you were trying to do this in your UI thread which will give error. – user1744952 Feb 07 '13 at 12:20
  • yes finally i acheived to do dat Thank you after rigorous days i finally completed this. Thank you so much :-) @user1744952 – ayansinha Feb 20 '13 at 10:05
0

You are running Network operations on the UI thread, try creating an AsyncTask do to the job, od a Thread. It's quite easy, all you have to do is move the code over.

Since you are using this on an adapter, meaning you will be populating a list of items, you may want to look into this. As you want to avoid regular issues with the listviews getView problem.

There are also open source libraries to help load urls into images. For instance UrlImageViewHelper from Koush helps you do this quite well. From your adapter simply call:

        if (holder.imgView != null) {
        if (null != objBean.getLink() && objBean.getLink().trim().length() > 0) {
            final ProgressBar pbar = holder.pbar;
            pbar.setVisibility(View.INVISIBLE);
            //---------CHANGES MADE FOR LOADING IMAGE----------//
            Log.d("IMAGE NULL----------", objBean.getLink());

            String linkurl = new URL(objBean.getLink().toString());
            UrlImageViewHelper.setUrlDrawable(holder.imgView, linkurl, R.id.your_default_drawable);

        }
daniel_c05
  • 11,438
  • 17
  • 60
  • 78