当解析JSONArray时,它在"改装"列表中最后一个



我通过get 获得json请求

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

如果你需要在一个单独的项目中显示每个国家,如何解析它。我在用Pojo。

@SerializedName("data")
@Expose
private ArrayList<String> data = null;

这是一个我使用改装来连接的活动,所有东西都来了,但只显示列表中的最后一个字段

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

这个适配器

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

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

在适配器的绑定文件夹中

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

最新更新