luongkhanh luongkhanh - 1 month ago 27
Android Question

How to fill data in Adapter and load to Gridview in StaggeredGrid Android

I have sent some days to fix but it's still same the problem. I can not fill data to StaggeredAdapter class and load data to GridView. I'm using AsyncTask class and doInBackground method to load json data.

My class:

public class StaggeredAdapter extends ArrayAdapter<HashMap<String,String>> {

ArrayList<HashMap<String, String>> manufacturersList;
private static final String TAG = "StaggeredAdapter";

private final LayoutInflater mLayoutInflater;
private final Random mRandom;
private static final SparseArray<Double> sPositionHeightRatios = new SparseArray<Double>();

public StaggeredAdapter(Context context, int textViewResourceId,
ArrayList<HashMap<String, String>> objects) {
super(context, textViewResourceId, objects);
this.mLayoutInflater = LayoutInflater.from(context);
this.mRandom = new Random();
this.manufacturersList = objects;
}

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

ViewHolder vh;
if (convertView == null) {
convertView = mLayoutInflater.inflate(R.layout.row_grid_item, parent, false);

vh = new ViewHolder();
vh.imgView = (DynamicHeightImageView) convertView.findViewById(R.id.imgView);
vh.price = (TextView) convertView.findViewById(R.id.price);
vh.name = (TextView) convertView.findViewById(R.id.name);

HashMap<String,String> map = new HashMap<>();
vh.price.setText(map.get(CategoryCarActivity.TAG_PRICE));
vh.name.setText(map.get(CategoryCarActivity.TAG_NAME));



convertView.setTag(vh);
} else {
vh = (ViewHolder) convertView.getTag();
}

double positionHeight = getPositionRatio(position);

vh.imgView.setHeightRatio(positionHeight);

// ImageLoader.getInstance().displayImage(getItem(position), vh.imgView);
return convertView;
}

static class ViewHolder {
DynamicHeightImageView imgView;
TextView name;
TextView price;
}

private double getPositionRatio(final int position) {
double ratio = sPositionHeightRatios.get(position, 0.0);
// if not yet done generate and stash the columns height
// in our real world scenario this will be determined by
// some match based on the known height and width of the image
// and maybe a helpful way to get the column height!
if (ratio == 0) {
ratio = getRandomHeightRatio();
sPositionHeightRatios.append(position, ratio);
Log.d(TAG, "getPositionRatio:" + position + " ratio:" + ratio);
}
return ratio;
}

private double getRandomHeightRatio() {
return (mRandom.nextDouble() / 2.0) + 1.0; // height will be 1.0 - 1.5
// the width
}
}


I have checked and debugged json have loaded success and I can get 3 ids from json in Gridview, but image, name and price can not get and display

Answer

Your code has two problems:

  • You are not getting the item from the ArrayAdapter superclass

  • You are not setting the values when a view is recycled

Your code should look like this:

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

        ViewHolder vh;
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.row_grid_item, parent, false);

            vh = new ViewHolder();
            vh.imgView = (DynamicHeightImageView) convertView.findViewById(R.id.imgView);
            vh.price = (TextView) convertView.findViewById(R.id.price);
            vh.name = (TextView) convertView.findViewById(R.id.name);

            convertView.setTag(vh);
        } else {
            vh = (ViewHolder) convertView.getTag();
        }

        HashMap<String,String> map = getItem(position);
        vh.price.setText(map.get(CategoryCarActivity.TAG_PRICE));
        vh.name.setText(map.get(CategoryCarActivity.TAG_NAME));

        double positionHeight = getPositionRatio(position);

        vh.imgView.setHeightRatio(positionHeight);

        // ImageLoader.getInstance().displayImage(getItem(position), vh.imgView);
        return convertView;
    }