微调器值未显示正确值(改造)

Spinner values are not showing correct value (retrofit)

第一次尝试使用 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

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

所以你要做的是遍历响应主体中的所有 ModelErrorType 对象并遍历 ModelErrorTypeBack 并将它们添加一个一个。

所以尝试更改此行

list.addAll(response.body());

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