我有数据模型类,我通过json下载照片。然后我想将这些照片添加到gridview中。但是我不能。

onCreate()中的此下载操作,

    ProductHandler.setProducts(products);
    final GridView gridView = (GridView) findViewById(R.id.gridView);

    AsyncTaskListener asyncTaskListener = new AsyncTaskListener() {
        @Override
        public void onCompleted(String result) {
            ArrayList<Product> products = ProductHandler.getProducts();

            String json = "";
            if(!Util.isNullOrEmpty(result)){
                json = Html.fromHtml(result).toString();
            }
            if(!Util.isNullOrEmpty(json) && Util.isJSONValid(json)){

                Log.e("json", json);
                try {

                    JSONArray jsonArray = new JSONArray(json);
                    for(int i = 0; i < jsonArray.length(); i++){
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        Product product = new Product();
                        product.setPhoto(jsonObject.getString("photo"));
                        products.add(product);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
            Log.e("after", String.valueOf(products.size()));
            ProductAdapter adapter = new ProductAdapter(context, products);
            gridView.setAdapter(adapter);
        }
    };
    RequestParams requestParams = new RequestParams();
    requestParams.add("api_key", Preferences.getSavedapiKey(context));
    TaskParams taskParams = new TaskParams(Constant.API_PATH + "presentation.php", ClientMethod.POST, requestParams);
    AsyncHttpClient asyncHttpClient = new AsyncHttpClient(asyncTaskListener);
    asyncHttpClient.execute(taskParams);


ProductHandler很简单;

public class ProductHandler {
    private static ArrayList<Product> products;

    public static synchronized ArrayList<Product> getProducts(){
        return products;
    }

    public static synchronized void setProducts(ArrayList<Product> products){
    ProductHandler.products = products;
    }
}


产品适配器;

public class ProductAdapter extends BaseAdapter {
private Context context;
private ArrayList<Product> products;

public ProductAdapter(Context context, ArrayList<Product> products){
    this.context = context;
    this.products = products;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ImageView imageView;

    if(convertView == null){
        imageView = new ImageView(context);
        imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
        imageView.setPadding(0, 0, 0, 0);
    } else{
        imageView = (ImageView) convertView;
    }

    Util.Image.stringToImageView(imageView, products.get(position).getPhoto());

    return imageView;
    }
}


产品在活动中定义为全局;

ArrayList<Product> products = new ArrayList<>();


因此,当它运行时,gridview为空。我不明白这个问题。谢谢。

最佳答案

请在适配器(ProductAdapter)中传递您的数量

@Override
public int getCount() {
    return products.size();// you are passing 0 here
}

关于android - 收到的数据不会膨胀到gridview,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42532506/

10-10 18:35
查看更多