java - Android StaggeredGrid中如何在Adapter中填充数据并加载到Gridview

标签 java android json

我已经花了几天时间来修复,但问题仍然相同。我无法将数据填充到 StaggeredAdapter 类并将数据加载到 GridView。我使用 AsyncTask 类和 doInBackground 方法来加载 json 数据。

我的类(class):

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> objects) {
    super(context, textViewResourceId, objects);
    this.mLayoutInflater = LayoutInflater.from(context);
    this.mRandom = new Random();
}

@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();
    }

    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
 }

我已经检查和调试json已经加载成功,并且我可以在Gridview中从json中获取3个id,但是图像,名称和价格无法获取和显示

最佳答案

您的代码有两个问题:

  • 您没有从 ArrayAdapter 父类(super class)获取该项目

  • 回收 View 时您没有设置值

您的代码应如下所示:

    @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;
    }

关于java - Android StaggeredGrid中如何在Adapter中填充数据并加载到Gridview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40377866/

相关文章:

java - 如何在java中拆分paint swing方法?

java - Eclipse 中的动态代码模板?

android - Nexus 5/10 有时只被亚行识别

javascript - Node js远程JSON对象返回未定义

java - Spring @Transactional 只有在调用者也是@Transactional 时才有效

Java notify() 在 wait() 之前运行?

java - 如何在android中的两个子 Activity 之间传输数据?

android - 无法显示 JSON 数组,JSONException : No value Error

jquery - 从Ajax调用获取数据,而不是字符串

javascript - JQuery 将 html 形式转换为正确格式的 JSON