GridView没有显示从Json检索到的数据

时间:2018-01-15 09:22:09

标签: java android gridview

我有一个gridview,应该显示从服务器检索的emojies作为url,我能够检索url并将其放在arraylist中,但是使用gridview适配器,根本没有显示图像,我'尝试通过手动编码for循环外的url进行调试,它显示了图像,这意味着我的适配器没有任何问题,当我尝试硬编码for循环内的url时,如arraylist.add(" url& #34;)没有图片显示,这里是我的代码,请告知图片未显示的原因,感谢您的帮助

BottomSheetDialog_Smiles.java

Communicator.getInstance().on("subscribe start", new Emitter.Listener() {

       @Override
       public void call(Object... args) {
           try {
               JSONDictionary response = (JSONDictionary) args[0];
               String str = response.get("emojiPack").toString();
               JSONArray emojies = new JSONArray(str);

               for (int i = 0; i < emojies.length(); i++) {

                   JSONObject response2 = (JSONObject) 
                   emojies.getJSONObject(i);

                   emojiModel = new EmojiModel((String) 
                   response2.get("urlFile"));
                   emojiUrl = emojiModel.getEmojiFile();
                   JSONDictionary t = 
               JSONDictionary.fromString(response2.toString());
                   emojiModel.init(t);
                   emojieModels.add(emojiModel);
               }

               ImageAdapter2 emojiAdapter = new 
               ImageAdapter2(getApplicationContext(), emojieModels);
               gridView2.setAdapter(emojiAdapter);

           } catch (JSONException e) {
               e.printStackTrace();
           }

       }
   });

ImageAdapter2.java

    public class ImageAdapter2 extends BaseAdapter {

    private Context context;
    private ArrayList<String> emojieImages = new ArrayList<String>();

    // Constructor
    public ImageAdapter2(Context context, ArrayList<String> 
    emojieImagesList) {
        this.context = context;
        this.emojieImages = emojieImagesList;
    }

    @Override
    public int getCount() {
        return emojieImages.size();
    }

    @Override
    public Object getItem(int position) {
        return emojieImages.get(position);
    }

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


    public class Holder {
        ImageView imageView;
    }


    @Override
    public View getView(final int position, View convertView, ViewGroup 
    parent) {

        Holder holder = new Holder();
        LayoutInflater inflater = (LayoutInflater) 
  getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        grid = inflater.inflate(R.layout.smiles_items_layout, null);

        holder.imageView = (ImageView) 
        grid.findViewById(R.id.smile_image_view);

        Picasso.with(getApplicationContext())
                .load(emojieImages.get(position))
                .fit()
                .centerInside()
                .into(holder.imageView);

        return grid;
       }
   }

EmojiModel.java

public class EmojiModel {
    private int id;
    private int price;
    public String urlFile;

public EmojiModel(String urlFile) {
    this.id=id;
    this.price=price;
    this.urlFile=urlFile;
}

public String getEmojiFile() {
    return urlFile;
}

public void init(JSONDictionary data){
    try{
        urlFile = (String) data.get("urlFile");
        id = Integer.parseInt((String) data.get("id"));
        price = Integer.parseInt((String) data.get("price"));

    }catch(Exception e){
        e.printStackTrace();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个

Picasso.with(this)
    .load(imageUrl)
    .fit()
    .centerInside()
    .into(imageViewFromUrl, new Callback() {
        @Override
        public void onSuccess() {
            Log.i(TAG, "succcess");
        }

        @Override
        public void onError() {
            Log.i(TAG, "error");
        }
    }
);