解析JSONArray在Retrofit列表中的位置是最后一个吗?

huangapple go评论66阅读模式
英文:

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

{&quot;data&quot;: [
    &quot;Черногория&quot;,
    &quot;Чехия&quot;,
    &quot;Чили&quot;
]}

How to parse it if you need to display each country in a separate item. I'm using Pojo.

@SerializedName(&quot;data&quot;)
    @Expose
    private ArrayList&lt;String&gt; 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&lt;Countries&gt; call, Response&lt;Countries&gt; response) {
            if (response.code() == 200) {
                Countries countries = response.body();
                if (countries != null) {
                    for (int x =0; x&lt;countries.getData().size(); x++) {
                        arrayList.add(countries);
                        viewAdapterCountry.notifyDataSetChanged();
                    }}}
        }

this adapter

private ArrayList&lt;Countries&gt; countryModels;
    Countries countryModel = countryModels.get(i);
            List&lt;String&gt; country = countryModel.getData();
            for (int x = 0; x&lt;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)
     
    }

huangapple
  • 本文由 发表于 2020年4月6日 15:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/61054706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定