lundi 2 décembre 2019

Set text color using if and else statement

SO i have this application that uses retrofit client to send a request to the ROS server and now my problem is that I am setting up my Status that if the status is "True" it will set to textcolor as GREEN and else as RED but when i execute it to my application it seems that its only executing the else statement it displays all red even if the status is true. Can anyone help me with this Im just a beginner.

Here is my Code:

    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;
    Handler handler = new Handler();
    int apiDelayed = 2 * 1000; //1 second=1000 milisecond, 5*1000=5seconds
    Runnable runnable;


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

    }

    public void onResume() {
        super.onResume();

        handler.postDelayed(runnable = new Runnable() {
            public void run() {
                //do your function;
                getSystemObject();
                handler.postDelayed(runnable, apiDelayed);
            }
        }, apiDelayed); // so basically after your getHeroes(), from next time it will be 5 sec repeated
    }

    @Override
    public void onPause() {
        super.onPause();
        handler.removeCallbacks(runnable); //stop handler when activity not visible
    }


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


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

                HardwareStatus.setText(response.body().getHardwareStatus());
                if (HardwareStatus.equals("false")) {
                    HardwareStatus.setTextColor(Color.RED);
                } else {
                    HardwareStatus.setTextColor(Color.GREEN);
                }

                //software
                SoftwareName.setText(response.body().getSoftwareName());
                SoftwareStatus.setText(response.body().getSoftwareStatus());

                if (SoftwareStatus.equals("true")) {
                    SoftwareStatus.setTextColor(Color.GREEN);
                } else {
                    SoftwareStatus.setTextColor(Color.RED);
                }

            }

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

                //services
                FlagName.setText(response.body().getFlagReport().toString());
            }

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

            }
        });

    }
}

& here is my model class:

package com.nera.rosbridgeclient.messages.robot_msgs;

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

import java.util.ArrayList;
import java.util.Arrays;

import static android.graphics.Typeface.BOLD;

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

Aucun commentaire:

Enregistrer un commentaire