gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 09:18:07 24 4
gpt4 key购买 nike

我已经花了几天时间来修复,但问题仍然相同。我无法将数据填充到 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com