0

hello all i have made one adpter class in that listCountry.size(); in getcount shows me the actual size that is of 500. But the getview call o*nly for 11 time*

public class ImageAdapter extends BaseAdapter {
        BitmapFactory.Options bmop;
        private ArrayList<String> listCountry;
        private Activity activity;

        public ImageAdapter(Activity activity, ArrayList<String> listCountry) {
            super();

            // this.listCountry = listCountry;
            this.listCountry = new ArrayList<String>();
            this.listCountry = listCountry;

            this.activity = activity;
            System.out.println("this is contry name " + this.listCountry);

        }

        @Override
        public int getCount() {
            System.out.println("len " + listCountry.size());// this shows 500
            return listCountry.size();
        }

        @Override
        public Object getItem(int position) {

            return listCountry.get(position);
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return arg0;
        }

        @Override
        public View getView(int arg0, View convertView, ViewGroup parent) {
            ViewHolder view;
            LayoutInflater inflator = activity.getLayoutInflater();
            if (convertView == null) {
                System.out.println(arg0+" this is from Adpter "+listCountry.get(arg0) );// this shows only first 11
                view = new ViewHolder();
                convertView = inflator.inflate(R.layout.item_grid_image, null);

                // Typeface typeface =
                // Typeface.createFromAsset(getAssets(),"fonts/DroidSerif.ttf");

                view.imgViewFlag = (ImageView) convertView
                        .findViewById(R.id.image);
                view.pb = (ProgressBar) convertView
                        .findViewById(R.id.progressBar1);

                // d.execute("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png");

                // view.imgViewFlag.setBackgroundResource(R.drawable.view_default);
                convertView.setTag(view);

            } else {
                view = (ViewHolder) convertView.getTag();
            }
            try {
                // view.txtViewTitle.setText(listCountry.get(position));
                // view.imgViewFlag.setImageResource(listFlag.get(position));
                //DownloadImageTask d = new DownloadImageTask(view.imgViewFlag);
            //  d.execute(listCountry.get(arg0));

            } catch (Exception e) {
                System.out.println("this is error " + e.getMessage());
            }

            return convertView;
        }

    }
Charles
  • 50,943
  • 13
  • 104
  • 142
Android
  • 8,995
  • 9
  • 67
  • 108
  • 2
    ListView recycles views. read this http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works – Raghunandan Apr 02 '14 at 09:32
  • plz explain it in detail – Android Apr 02 '14 at 09:32
  • 1
    As you are using recycle view `convertView` hence `getView()` called only visible rows count on your screen. + another reference view. – user370305 Apr 02 '14 at 09:33
  • mean? not able to understand – Android Apr 02 '14 at 09:34
  • how did you know getView method is call only 11 times?it calls only when new view required to display – Imtiyaz Khalani Apr 02 '14 at 09:34
  • 1
    Suppose there is space of 10 items to display. `getView()` is called 10 times. an upon scroll the views are recycled. Note visible views are not recycled – Raghunandan Apr 02 '14 at 09:35
  • see my updated System.out.println(arg0+" this is from Adpter "+listCountry.get(arg0) );// this shows only first 11 this is in getview – Android Apr 02 '14 at 09:35
  • @Pragna bcoz at first only 11 rows are displayed. tried looking at the log by scrolling so that one row goes off screen. You will know what i meant by recycling – Raghunandan Apr 02 '14 at 09:35
  • Ok, remove if - else condition and only use code inside of if condition part. Now your `getView()` will be called size of data array.. – user370305 Apr 02 '14 at 09:36
  • @raghu.. my problem is that i am downloading img ok? in getview.. so prob is that i am getting overlap the imag by another 11 again by another 11 mean each time only loads 11 images – Android Apr 02 '14 at 09:36
  • 1
    @Pragna use lazy loading with caching. Downlaod the sample @ http://developer.android.com/training/displaying-bitmaps/load-bitmap.html – Raghunandan Apr 02 '14 at 09:37
  • @Pragna - Try my above comment it will solve your problem. – user370305 Apr 02 '14 at 09:38
  • removing if else not working...:( – Android Apr 02 '14 at 09:51

1 Answers1

0

It will only populate the list with the items that can fit your screen size and are visible to you. if you are using large images then you can feel the lag caused by this method

Aditya_Anand
  • 525
  • 7
  • 17