有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java在解析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));
            }

共 (1) 个答案

  1. # 1 楼答案

    我认为你是在一次添加所有数据,而不是逐个添加 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)
    
        }