mercredi 27 novembre 2019

Setting Enable or Disable instead of True or False using IF and ELSE statement

So basically what I'm trying to do is that I'm sending a request using retrofit to get a data from my server and I was able to do it. My problem is that I want my program to display "Enable" or "Disable" instead of True or False and change its color based on their status ex: green for enable and red for disable. But its only displaying "Disabled" and it isn't incremented.

Here is my model class from the json data

    public class ObjectList {

    @SerializedName("hardware")
    @Expose
    public Objects[] hardware;
    @SerializedName("software")
    @Expose
    public Objects[] software;

    /**
     * No args constructor for use in serialization
     */
    public ObjectList() {
    }

    /**
     * @param software
     * @param hardware
     */
    public ObjectList(Objects[] hardware, Objects[] software) {
        super();
        this.hardware = hardware;
        this.software = software;
    }

    public Objects[] getHardware() {
        return hardware;
    }

    public void setHardware(Objects[] hardware) {
        this.hardware = hardware;

    }

    public Objects[] getSoftware() {
        return software;
    }

    public void setSoftware(Objects[] software) {
        this.software = software;
    }


    public String getHardwareName() {
        String HWname = "";
        for (int i = 0; i < hardware.length; i++) {
            Objects item = hardware[i];
            HWname += ((Objects) item).name + "\n\n";

        }

        return HWname;

    }

    public String getHardwareStatus() {
        String HWstatus = "";
        for (int i = 0; i < hardware.length; i++) {
            Objects item = hardware[i];
            HWstatus += ((Objects) item).status + "\n\n";

        }
        return HWstatus;
    }

    public String getSoftwareName() {
        String SWname = "";
        for (int i = 0; i < software.length; i++) {
            Objects item = software[i];
            SWname += ((Objects) item).name + "\n\n";
        }
        return SWname;
    }

    public String getSoftwareStatus() {
        String SWstatus = "";
        for (int i = 0; i < software.length; i++) {
            Objects item = software[i];
            SWstatus += ((Objects) item).status + "\n\n";
        }

        return SWstatus;
    }

}

Here is my SystemStatus class

 package com.nerarobotics.robotutility.fragments

public class SystemStatusFragment extends Fragment { private static final String ARG_SECTION_NUMBER = "section_number";

private String baseURL = "http://10.24.204.231:11211/ros/";
private View currentView;
private PageViewModel pageViewModel;
private TextView HardwareName, HardwareStatus, SoftwareName, SoftwareStatus, FlagName;
private Runnable mRepeatRunnable;
private final static int UPDATE_INTERVAL = 2000;


public static SystemStatusFragment newInstance(int index) {
    SystemStatusFragment fragment = new SystemStatusFragment();
    Bundle bundle = new Bundle();
    bundle.putInt(ARG_SECTION_NUMBER, index);
    fragment.setArguments(bundle);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    pageViewModel = ViewModelProviders.of(this).get(PageViewModel.class);
    int index = 1;
    if (getArguments() != null) {
        index = getArguments().getInt(ARG_SECTION_NUMBER);
    }
    pageViewModel.setIndex(index);
}

@Override
public View onCreateView(
        @NonNull LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    currentView = inflater.inflate(R.layout.fragment_system_status, null);

    HardwareName = currentView.findViewById(R.id.hardware_name);
    HardwareStatus = currentView.findViewById(R.id.hardware_status);
    SoftwareName = currentView.findViewById(R.id.software_name);
    SoftwareStatus = currentView.findViewById(R.id.software_status);
    FlagName = currentView.findViewById(R.id.flagreport_name);
    getSystemObject();

    final Handler handler = new Handler();
    Runnable runnable = new Runnable() {
        @Override
        public void run() {
            handler.postDelayed(this,2000);
        }
    }; handler.postDelayed(runnable,2000 );


    return currentView;

}

public void getSystemObject() {
    Retrofit Systemretrofit = new Retrofit.Builder()
            .baseUrl(baseURL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    WebApi jsonAPI = Systemretrofit.create(WebApi.class);
    Call<ObjectList> callSystem = jsonAPI.getNameStatus();
    callSystem.enqueue(new Callback<ObjectList>() {
        @Override
        public void onResponse(Call<ObjectList> callSystem, Response<ObjectList> response) {

            if (!response.isSuccessful()) {
                HardwareName.setText("Code: " + response.code());
                return;
            }


            HardwareName.setText(response.body().getHardwareName ());


            if (response.body().getHardwareStatus().equals("true")) {
                HardwareStatus.setText("Enable");
            } else {

                HardwareStatus.setText("Disable");
            }

        }

        @Override
        public void onFailure(Call<ObjectList> callSystem, Throwable t) {
            HardwareName.setText(t.getMessage());
            SoftwareName.setText(t.getMessage());
        }
    });

    final Call<FlagReportStamped> callFlagReport = jsonAPI.getReportID();
    callFlagReport.enqueue(new Callback<FlagReportStamped>() {
        @Override
        public void onResponse(Call<FlagReportStamped> call, Response<FlagReportStamped> response) {

            if (!response.isSuccessful()) {
                FlagName.setText("Code: " + response.code());
                return;
            }

            FlagName.setText(response.body().getFlagReport().toString());

        }

        @Override
        public void onFailure(Call<FlagReportStamped> call, Throwable t) {
            FlagName.setText(t.getMessage());

        }
    });

}

}

Aucun commentaire:

Enregistrer un commentaire