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

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

When parsing JSONArray comes last in the list of Retrofit?

问题

I get json request through GET

  1. { "data": [
  2. "Черногория",
  3. "Чехия",
  4. "Чили"
  5. ]}

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

  1. @SerializedName("data")
  2. @Expose
  3. 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

  1. public void onResponse(Call<Countries> call, Response<Countries> response) {
  2. if (response.code() == 200) {
  3. Countries countries = response.body();
  4. if (countries != null) {
  5. for (int x = 0; x < countries.getData().size(); x++) {
  6. arrayList.add(countries);
  7. viewAdapterCountry.notifyDataSetChanged();
  8. }
  9. }
  10. }
  11. }

this adapter

  1. private ArrayList<Countries> countryModels;
  2. Countries countryModel = countryModels.get(i);
  3. List<String> country = countryModel.getData();
  4. for (int x = 0; x < country.size(); x++) {
  5. viewHolder.button.setText(country.get(x));
  6. }
英文:

I get json request through GET

  1. {&quot;data&quot;: [
  2. &quot;Черногория&quot;,
  3. &quot;Чехия&quot;,
  4. &quot;Чили&quot;
  5. ]}

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

  1. @SerializedName(&quot;data&quot;)
  2. @Expose
  3. 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

  1. public void onResponse(Call&lt;Countries&gt; call, Response&lt;Countries&gt; response) {
  2. if (response.code() == 200) {
  3. Countries countries = response.body();
  4. if (countries != null) {
  5. for (int x =0; x&lt;countries.getData().size(); x++) {
  6. arrayList.add(countries);
  7. viewAdapterCountry.notifyDataSetChanged();
  8. }}}
  9. }

this adapter

  1. private ArrayList&lt;Countries&gt; countryModels;
  2. Countries countryModel = countryModels.get(i);
  3. List&lt;String&gt; country = countryModel.getData();
  4. for (int x = 0; x&lt;country.size(); x++){
  5. viewHolder.button.setText(country.get(x));
  6. }

答案1

得分: 1

我认为你一次性添加了所有数据,而不是逐个添加
arrayList.add(countries);你应该这样做 arrayList.add(countries.getData().get(x)); 然后在循环外部你应该写 viewAdapterCountry.notifyDataSetChanged();

在适配器的 bindViewHolder 方法中:

  1. override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
  2. val item = list?.get(position)
  3. holder.button.setText(item)
  4. }
英文:

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

  1. override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
  2. val item = list?.get(position)
  3. holder.button.setText(item)
  4. }

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:

确定