-1

I had a gridview to show pictures dynamically. When picture is two, convertview of the position=1 is always not null? why? I don't ask why getView() is called many time.My question is why position=1 is always not null when it called at the first time, and the other positions are the null?

    <Gridview
        android:id="@+id/tgv_image_select"
        android:layout_width="270dip"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dip"
        android:layout_marginLeft="@dimen/margin_10dp"
        android:gravity="center"
        android:horizontalSpacing="10dip"
        android:numColumns="4"
        android:verticalSpacing="10dip" />


  public class SelectImageGridViewAdapter extends BaseListAdapter<ImageItem> {

   private LoadFrescoImage loadFrescoImage;

public SelectImageGridViewAdapter(Context context) {

    super(context);
    loadFrescoImage = new LoadFrescoImage(Application.getInstance());
}

public void addImageItem(ImageItem imageItem) {
    if (imageItem == null) return;
    getData().add(imageItem);
    notifyDataSetChanged();
}

public void addImageItemLists(List<ImageItem> lists) {
    if(lists == null) return;
    getData().addAll(lists);
    notifyDataSetChanged();
}

public void removeItem(int position) {
    getData().remove(position);
    notifyDataSetChanged();
}

public void reloadImageItem(List<ImageItem> lists) {
    if (lists == null) return;
    setData(lists);
    notifyDataSetChanged();
}

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

    Holder holder;
    if (convertView == null) {
        holder = new Holder();
        convertView = mInflater.inflate(R.layout.grid_view_item, null);
        holder.mSelectImage = (ImageView)     
          convertView.findViewById(R.id.iv_icon);
        convertView.setTag(holder);
    } else {
        holder = (Holder) convertView.getTag();
    }
    if (getData().size() > 0 && position != getData().size()) {
        holder.mSelectImage.setVisibility(View.VISIBLE);
        ImageItem imageItem = getData().get(position);
        if (imageItem != null) {

                String imguri = BitmapUtil.IMAGE_URI_SUFFIX_SDCARD + imageItem.getImageUri().getPath();
                Uri ImgUri = Uri.parse(imguri);
                loadFrescoImage.LoadImage(ImgUri,holder.mSelectImage);
            }
        }
    } else {
        if (position == Fragment.DEFAULT_PHOTO_COUNT) {
            holder.mSelectImage.setVisibility(View.GONE);
        } else {
            holder.mSelectImage.setVisibility(View.VISIBLE);
            holder.mSelectImage.setImageURI(BusinessUtil.getResourceUri(convertView.getContext(),R.drawable.guild_icon_img));
        }

    }

    return convertView;
}

class Holder {
    private ImageView mSelectImage;
}
  }



 when I choose 2 picture The log is :
    mytestnew   convertView = nullposition =0
    mytestnew   convertView = android.widget.FrameLayout{3f51f239 V.E.....      
        ......I. 0,0-0,0 #7f0b0117 app:id/fl_item}position =0
                      ....
    mytestnew   convertView = android.widget.FrameLayout{3f51f239 V.E.....   
    ......I. 0,0-0,0 #7f0b0117 app:id/fl_item}position =1

But,When I choose more than 2 pictures,the log is :
   mytestnew   convertView = nullposition =0
   mytestnew   convertView = android.widget.FrameLayout{3921e34b V.E.....       
        ........ 0,0-180,180 #7f0b0117 app:id/fl_item}position =0
                       ......   
   mytestnew   convertView = android.widget.FrameLayout{21daf777 V.E.....    
       ......I. 0,0-0,0 #7f0b0117 app:id/fl_item}position =1
   mytestnew   convertView = null position =2
   mytestnew   convertView = null position =3

Why position =1 is always not null, and the other position is null?

judyzha
  • 367
  • 1
  • 4
  • 11
  • please post full code of adapter – Naveen Tamrakar Jun 06 '16 at 13:10
  • Possible duplicate of [custom listview adapter getView method being called multiple times, and in no coherent order](http://stackoverflow.com/questions/2618272/custom-listview-adapter-getview-method-being-called-multiple-times-and-in-no-co) – Mike M. Jun 06 '16 at 13:10
  • The answer there is basically saying that it doesn't - and shouldn't - matter. If `convertView` is `null`, inflate one. There are no guarantees about what you'll be passed in `getView()`. – Mike M. Jun 06 '16 at 13:12
  • Do not make assumptions about the number of times `getView()` is called, when it is called, or in what order it is called. – Karakuri Jun 06 '16 at 13:59
  • @MikeM. I study the source code, if position =1 is the first time to init, it should be null like the other positions. And my question is why position=1 is always not null, does I have a wrong understanding? – judyzha Jun 07 '16 at 01:46
  • @Karakuri I don't ask why getView() is called my times. My question is why position=1 is always not null when init at the first time? – judyzha Jun 07 '16 at 01:52
  • @NaveenTamrakar I have add all adapter core code,thks – judyzha Jun 07 '16 at 02:18
  • I don't know what code you're looking at, but there's nothing that says that if `getView()` hasn't been called for a certain position, then the `convertView` must be `null`. In your case, it's obviously recycling the `View` it got when calling for position 0. Now that I say that, I guess you probably do have a misunderstanding of how an `AdapterView` works. `View`s are recycled. If you have 1000 items in your list, but only 10 fit on-screen, you're only gonna have about 10 `View`s (plus maybe one or two more). Have a look at [this post](http://stackoverflow.com/questions/11945563). – Mike M. Jun 07 '16 at 02:41
  • @judyzha `convertView` is not null if the `AdapterView` is recycling a row View that was previously created. It has nothing to do with the position, and it can call `getView()` at times you may not realize; for example, it might call `getView()` just to measure a row and then recycle the View. The point of my statement above is that your code should not care about this at all. – Karakuri Jun 07 '16 at 02:52
  • 1
    @MikeM. Thank you very much. the post is what I really want, and I have known my bug~ – judyzha Jun 08 '16 at 02:13

1 Answers1

0

Reference this post for detail grideview recycling mechanism. The code should not care about converview. Thanks for @MikeM and @Karakuri

Community
  • 1
  • 1
judyzha
  • 367
  • 1
  • 4
  • 11