0

I have a grid view its showing some images from SD card it showing properly but only when I am scrolling it loading images again and again that is why scrolling is too slow I don't know why it loading images every time when I am scrolling (images that are not showing inside the screen.)

My code

        // create a new ImageView for each item referenced by the Adapter
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (c.moveToFirst())
        {
            do 
            {          
                if (convertView == null) 
                {             
                    convertView = mInflater.inflate(R.layout.grid_row_view, null);
                    holder = new ViewHolder();
                    holder.ImgThumb  = (ImageView) convertView.findViewById(R.id.imgThumb);
                    holder.Viewcover = (ImageView) convertView.findViewById(R.id.cover);
                    holder.PdfUrl    = (TextView) convertView.findViewById(R.id.hiddenPdfUrl);
                    convertView.setTag(holder);
                } 
                else 
                {
                    holder = (ViewHolder) convertView.getTag();
                }

                String imagePath = Environment.getExternalStorageDirectory().toString() + "/ICA Faculty/";

                holder.ImgThumb.setImageDrawable(Drawable.createFromPath(imagePath + imgUrl.get(position)));
                holder.Viewcover.setImageResource(R.drawable.book_cover);
                holder.PdfUrl.setText(pdfUrl.get(position));

            }  while (c.moveToNext());
        }

        return convertView;        
    } 

    private class ViewHolder {
        ImageView ImgThumb;
        ImageView Viewcover;
        TextView PdfUrl;
    }
Cœur
  • 37,241
  • 25
  • 195
  • 267
A J
  • 4,542
  • 5
  • 50
  • 80
  • `if (c.moveToFirst())` <= what for all the cursor stuff is in getView ? you don't even use it (Cursor c) in your loop ... – Selvin Apr 16 '13 at 09:39
  • "when I am scrolling it loading images again and again" this is general act of gridView and ListView... it calls getView method for every raw. And you are reloading on your getView method everytime over again... – yahya Apr 16 '13 at 09:42

2 Answers2

1
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    //final boolean result = ( position == 0 ) ? c.moveToFirst() : c.moveToNext();


    if (c.moveToPosition(position))
    {
        if (convertView == null) 
        {             
            convertView = mInflater.inflate(R.layout.grid_row_view, null);
            holder = new ViewHolder();
            holder.ImgThumb  = (ImageView) convertView.findViewById(R.id.imgThumb);
            holder.Viewcover = (ImageView) convertView.findViewById(R.id.cover);
            holder.PdfUrl    = (TextView) convertView.findViewById(R.id.hiddenPdfUrl);
            convertView.setTag(holder);
        } 
        else 
        {
            holder = (ViewHolder) convertView.getTag();
        }

        String imagePath = Environment.getExternalStorageDirectory().toString() + "/ICA Faculty/";

        holder.ImgThumb.setImageDrawable(Drawable.createFromPath(imagePath + imgUrl.get(position)));
        holder.Viewcover.setImageResource(R.drawable.book_cover);
        holder.PdfUrl.setText(pdfUrl.get(position));

    }
    return convertView;
}

Also Inside getCount method of your adapter return c.getCount();

Triode
  • 11,309
  • 2
  • 38
  • 48
  • Error `Multiple markers at this line - This method must return a result of type View` *In first line* `public View getView(int position, View convertView, ViewGroup parent) ` – A J Apr 16 '13 at 09:42
  • Another thing @Rajesh when I am loading this activity screen geeting white some 10 - 12 sec depend on images why can u tell me – A J Apr 16 '13 at 09:48
  • yes when this activity is loading it showing a white screen for 10 to 12 sec – A J Apr 16 '13 at 09:52
  • 1
    It might be the GridView Background try to change it to some other color. – Triode Apr 16 '13 at 09:53
  • android:layout_marginTop="38sp" in GridView Description in layout file this can be the issue. – Triode Apr 16 '13 at 09:59
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/28301/discussion-between-rajesh-cp-and-bumba) – Triode Apr 16 '13 at 09:59
  • 2
    Sorry and don try to assign all the task to me. And am here to give you some hint. Try to learn this kinda things yourself this will help you in the future and stack overflow wont open the final door for you. – Triode Apr 16 '13 at 10:01
1

That's because you're loading all the images for every image... If you're using a cursor, consider using a SimpleCursorAdapter.

Aballano
  • 1,037
  • 12
  • 19