2016-11-02 4 views
0

修正するまでに数日を要しましたが、それでも問題は同じです。私はStaggeredAdapterクラスにデータを埋め込むことができず、データをGridViewに読み込むことができません。私はAsyncTaskクラスとdoInBackgroundメソッドを使用してjsonデータをロードしています。StaggeredGridのAdapterでデータを入力してGridviewに読み込む方法

私のクラス:

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を取得することができますが、画像、名称および価格は得ることができないと表示

答えて

1

あなたコードは2つの問題があります:あなたは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; 
    } 
+0

は私の問題を検出するために、私は試してみて、あなたに戻って – luongkhanh

+0

ようになるありがとうあなたは、ビューがリサイクルされたときに値を設定していないと言った、それはうまくいった、ありがとう! – luongkhanh