我已经发送了一些日子来修复,但问题仍然存在。我无法将数据填充到StaggeredAdapter类并将数据加载到GridView。我使用AsyncTask类和doInBackground方法加载json数据。
我的课程:
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,但图像,名称和价格无法获取并显示
答案 0 :(得分:1)
您的代码有两个问题:
您没有从ArrayAdapter
超类
在回收视图时,您没有设置值
您的代码应如下所示:
@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;
}