我有数据模型类,我下载json的照片。然后我想在gridview中添加theese照片。但我不能。
onCreate(),
中的此下载操作Thread.exit()
ProductHandler很简单;
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);
ProductAdapter;
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;
}
}
因此,当它运行时,gridview为空。我无法理解这个问题。谢谢。
答案 0 :(得分:1)
请在适配器(ProductAdapter)
@Override
public int getCount() {
return products.size();// you are passing 0 here
}