【问题标题】:Spinner values are not showing correct value (retrofit)微调器值未显示正确值(改造)
【发布时间】:2020-07-03 06:54:36
【问题描述】:

第一次尝试使用 Spinner。 Spinner 值未显示确切值,它显示的是命名空间。

我在改造的帮助下获取数据。之后我不知道该怎么做,但尝试在互联网代码的帮助下。

final Call<List<ModelErrorType>> errorTypeList = CustomersAPI.getSpinnerErrrorService().errorTypeList();
        errorTypeList.enqueue( new Callback<List<ModelErrorType>>() {
            @Override
            public void onResponse(Call<List<ModelErrorType>> call, Response<List<ModelErrorType>> response) {
//                Log.w("2.0 getFeed >> ",new GsonBuilder().setPrettyPrinting().create().toJson(response));

                Log.d( "JSON  LIST", new GsonBuilder().setPrettyPrinting().create().toJson( response ) );

                Toast.makeText( getContext(), response.toString(), Toast.LENGTH_SHORT ).show();


                List<ModelErrorType> list = new ArrayList<ModelErrorType>();

                ArrayAdapter<ModelErrorType> dataAdapter = new ArrayAdapter<ModelErrorType>
                        (getContext(), android.R.layout.simple_spinner_item, list);

                list.addAll(response.body());

                dataAdapter.notifyDataSetChanged();

                dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                spinner.setAdapter(dataAdapter);


            }

我的json值结果是


[
   {
      "status":"true",
      "message":"fetch successfully!",
      "data":[
         {
            "id":"1",
            "Name":"Blue Light Not Blinking"
         },
         {
            "id":"2",
            "Name":"No Light Comming"
         }
      ]
   }
]

我的模型正在关注我在 Json To Pojo 的帮助下得到了它


package com.example.iqhut;

import java.util.List;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import androidx.annotation.NonNull;

public class ModelErrorType {

    @SerializedName("status")
    @Expose
    private String status;
    @SerializedName("message")
    @Expose
    private String message;
    @SerializedName("data")
    @Expose
    private List<ModelErrorTypeBack> data = null;

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<ModelErrorTypeBack> getData() {
        return data;
    }

    public void setData(List<ModelErrorTypeBack> data) {
        this.data = data;
    }


}


package com.example.iqhut;

import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;

import androidx.annotation.NonNull;

public class ModelErrorTypeBack {

    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("Name")
    @Expose
    private String name;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @NonNull
    @Override
    public String toString() {
        return name;
    }
}

改造代码如下。

 public static SpinnerErrrorService spinnerErrrorService= null;

    public static SpinnerErrrorService getSpinnerErrrorService() {
        if (spinnerErrrorService == null) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl( url )
                    .addConverterFactory( GsonConverterFactory.create() )
                    .build();

            spinnerErrrorService = retrofit.create( SpinnerErrrorService.class );
        }

        return spinnerErrrorService;
    }

    public interface SpinnerErrrorService {
        @GET("/iqhut/api/errorcategory.php?")
        Call<List<ModelErrorType>> errorTypeList();
    }

下拉菜单显示模型命名空间 com.example.iqhut.ModelErrorType@52453b

【问题讨论】:

    标签: java android retrofit spinner


    【解决方案1】:

    此处的响应正文是 ModelErrorType 列表,您正尝试将其添加到微调器数据列表中,但微调器仅接受字符串列表。

    所以你要做的就是遍历响应体中的所有ModelErrorType对象,然后遍历ModelErrorTypeBack并一一添加。

    所以尝试改变这一行

    list.addAll(response.body());

    for(ModelErrorType errorType : response.body()){
        for(ModelErrorTypeBack errorTypeBack : errorType.getData){
            list.add(errorTypeBack.getName());
        }
    }
    

    【讨论】:

    • list.add(errorTypeBack.getName());显示错误,它说的是必需类型。模型错误类型提供的字符串
    • 将列表和 dataAdapter 类型从 ModelErrorType 更改为 String
    • 你能帮我多一点吗?比如如何选择和获取name的ID值。
    • 要选择并获取您需要为微调器创建自定义适配器的名称值,请查看此链接:stackoverflow.com/questions/10678521/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 2013-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多