Search code examples
javaandroidretrofitspinner

Spinner values are not showing correct value (retrofit)


Trying to use Spinner First time. Spinner value is not showing exact value, It's showing namespace.

I am fetching data with help of retrofit. after that I am not sure how to do but attempted with help of internet codes.

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


            }

My json value result is


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

My Models are following I got it with help of 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;
    }


}

and


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

Retrofit code is following.

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

Dropdown is showing model namespace com.example.iqhut.ModelErrorType@52453b


Solution

  • The Response body here is a list of ModelErrorType and you are trying to add it to the spinner data list, but the spinner accepts only list of String.

    So what you have to do is to loop over all ModelErrorType objects in the response body and loop over ModelErrorTypeBack and add them one by one.

    So try changing this line

    list.addAll(response.body());

    to

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