英文:
When parsing JSONArray comes last in the list of Retrofit?
问题
I get json request through GET
{ "data": [
    "Черногория",
    "Чехия",
    "Чили"
]}
How to parse it if you need to display each country in a separate item. I'm using Pojo.
@SerializedName("data")
@Expose
private ArrayList<String> data = null;
this is an activity in which I use retrofit to connect, everything comes, but displays only the last field in the lists
public void onResponse(Call<Countries> call, Response<Countries> response) {
    if (response.code() == 200) {
        Countries countries = response.body();
        if (countries != null) {
            for (int x = 0; x < countries.getData().size(); x++) {
                arrayList.add(countries);
                viewAdapterCountry.notifyDataSetChanged();
            }
        }
    }
}
this adapter
private ArrayList<Countries> countryModels;
Countries countryModel = countryModels.get(i);
List<String> country = countryModel.getData();
for (int x = 0; x < country.size(); x++) {
    viewHolder.button.setText(country.get(x));
}
英文:
I get json request through GET
{"data": [
    "Черногория",
    "Чехия",
    "Чили"
]}
How to parse it if you need to display each country in a separate item. I'm using Pojo.
@SerializedName("data")
    @Expose
    private ArrayList<String> data = null;
this is an activity in which I use retrofit to connect, everything comes, but displays only the last field in the lists
public void onResponse(Call<Countries> call, Response<Countries> response) {
            if (response.code() == 200) {
                Countries countries = response.body();
                if (countries != null) {
                    for (int x =0; x<countries.getData().size(); x++) {
                        arrayList.add(countries);
                        viewAdapterCountry.notifyDataSetChanged();
                    }}}
        }
this adapter
private ArrayList<Countries> countryModels;
    Countries countryModel = countryModels.get(i);
            List<String> country = countryModel.getData();
            for (int x = 0; x<country.size(); x++){
                viewHolder.button.setText(country.get(x));
            }
答案1
得分: 1
我认为你一次性添加了所有数据,而不是逐个添加
arrayList.add(countries);你应该这样做 arrayList.add(countries.getData().get(x)); 然后在循环外部你应该写 viewAdapterCountry.notifyDataSetChanged();
在适配器的 bindViewHolder 方法中:
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    val item = list?.get(position)
    holder.button.setText(item)
}
英文:
I think you are adding all the data at once instead of one by one
arrayList.add(countries);you should do  arrayList.add(countries.getData().get(x)); then outside the loop you should write viewAdapterCountry.notifyDataSetChanged();
in bindivewholder of adapter
    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        val item = list?.get(position)
        holder.button.setText(item)
     
    }
专注分享java语言的经验与见解,让所有开发者获益!



评论