Issue with custom gridview Android

I have developed an app in which I display data in Gridview. All data comes from local storage. I am able to display each data correctly and there are no issues with it. But when I have scroll the Gridview and goto the bottom and getback to Top,it changes position. And sometimes when I scroll down and getback to Top, a blank screen appears on screen;no data found at all!

So I thought that there is issue with getView(). I am unable to figure out the problem

Code of getView():

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    ViewHolder viewholder;
    // LayoutInflater inflator = activit.getLayoutInflater();
    if(convertView == null) {
        // recycled view is null so create it.
        viewholder = new ViewHolder();
        convertView = inflator.inflate(R.layout.gridviewrow, null);
        viewholder.imgvGridicon = (ImageView) convertView.findViewById(R.id.imgvGridicon);
        viewholder.txtGridItemlabel = (TextView) convertView.findViewById(R.id.txtGridItemlabel);
        convertView.setTag(viewholder); 
    } else {
        viewholder = (ViewHolder) convertView.getTag();      
    }  
    if ((lstpinfo.get(position).appname.toString()) != null) {
        viewholder.imgvGridicon.setImageDrawable((lstpinfo.get(position).icon));
        viewholder.txtGridItemlabel.setText(lstpinfo.get(position).appname.toString());
    }
    return convertView;
}

Update:: Intitalize of inflater::

private LayoutInflater inflator;

     private ArrayList<PInfo> lstpinfo = new ArrayList<PInfo>();
     public GridViewAdapter(Context cntx, ArrayList<PInfo> lstpinfo) {

            activit = cntx;
            inflator = LayoutInflater.from(cntx);
            this.lstpinfo = lstpinfo;

        }

Answer

You have to set your view height with fixed value. There’s a scrolling bug in android gridview with different heights. Please see this for reference.

Leave a Reply

Your email address will not be published. Required fields are marked *