gpt4 book ai didi

java - GridView 不显示从 Json 检索的数据

转载 作者:行者123 更新时间:2023-12-02 11:45:13 25 4
gpt4 key购买 nike

我有一个 gridview,它应该显示从服务器检索的表情符号作为网址,我能够检索网址并将其放入数组列表中,但是使用 gridview 适配器,根本没有图像显示,我已经尝试过通过在 for 循环外部手动编码 url 进行调试,它显示了图像,这意味着我的适配器没有任何问题,而且当我尝试对 for 循环内的 url 像 arraylist.add("the url") 进行硬编码时,也不会出现图像,这是我的代码,请告知为什么图像不显示,感谢您的帮助

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();
}
}
}

最佳答案

试试这个

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");
}
}
);

关于java - GridView 不显示从 Json 检索的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48260024/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com