如何从url获取图像到arrayList

时间:2018-01-14 09:45:16

标签: java android gridview arraylist

我试图显示从网址中读取的图片,它们不仅仅是一张图片,所以我必须将它们全部放在一个arraylist中,然后让图像显示在gridview中,出于某种原因,它和#39;没有显示任何内容,gridview完全空白,请告诉我我做错了什么。

BottomSheetDialog_Smiles.java

scanf

EmojiAdapter.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();
Picasso.with(getApplicationContext()).load(emojiUrl);
JSONDictionary t = JSONDictionary.fromString(response2.toString());
emojiModel.init(t);
emojieModels.add(new EmojiModel(emojiUrl));
                }

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


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

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

EmojiModel.Java

public class EmojiAdapter extends ArrayAdapter<EmojiModel> {

Context context;
ArrayList<EmojiModel> list = new ArrayList<>();

public EmojiAdapter(Context context,ArrayList<EmojiModel> list) {
    super(context, R.layout.smiles_items_layout, list);
    this.context = context;
    this.list = list;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater o = 
(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View v = o.inflate(R.layout.gifts_layout_2, parent , false);

    ImageView imageView = (ImageView) v.findViewById(R.id.smile_image_view);


imageView.setImageResource(Integer.parseInt((list.get(position)).urlFile));

    return v;
    }
}

2 个答案:

答案 0 :(得分:3)

显然这行代码不起作用:

imageView.setImageResource(Integer.parseInt((list.get(position)).urlFile));

而不是只使用滑行或piccaso加载图片。 首先将此行添加到您的gradle文件中:

  implementation 'com.github.bumptech.glide:glide:4.5.0'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.5.0'

然后代替上面的行,只需写: Glide.with(上下文).load(list.get(位置))urlFile).into(ImageView的);

picaso库也很相似

也会以这种方式更改您的适配器:

public class EmojiAdapter extends BaseAdapter {

    Context context;
    ArrayList<EmojiModel> list = new ArrayList<>();

    public EmojiAdapter(Context context,ArrayList<EmojiModel> list) {
        super(context, R.layout.smiles_items_layout, list);
        this.context = context;
        this.list = list;
    }
    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater o =
                (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = o.inflate(R.layout.gifts_layout_2, parent , false);

        ImageView imageView = (ImageView) v.findViewById(R.id.smile_image_view);


        imageView.setImageResource(Integer.parseInt((list.get(position)).urlFile));

        return v;
    }

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

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

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

答案 1 :(得分:1)

在适配器中使用Picasso显示图像

   @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater o =
 (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = o.inflate(R.layout.gifts_layout_2, parent , false);

        ImageView imageView = (ImageView) v.findViewById(R.id.smile_image_view);

        Picasso.with(getApplicationContext()).load(list.get(position).getEmojiUrl()).into(imageView);

        return v;
        }
    }