【问题标题】:When parsing JSONArray comes last in the list of Retrofit?当解析 JSONArray 在 Retrofit 列表中排在最后时?
【发布时间】:2020-07-18 03:36:48
【问题描述】:

我通过 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));
            }

【问题讨论】:

  • 你不是总是用viewHolder.button.setText(country.get(x)); 覆盖按钮文本,所以列表中的最后一个条目就是你看到的那个吗?
  • 这样做,viewHolder.button.setText(country.toString());显示所有内容,但不是在每个按钮中,而是全部在一个中

标签: java android json parsing retrofit


【解决方案1】:

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

在适配器的 bindivewholder 中

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

        holder.button.setText(item)

    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 1970-01-01
    相关资源
    最近更新 更多