Reputation: 115
I have a status in my json object that was set to boolean. My main problem is that in every handled time let's say 5 secs it will add another set of status which is the previous status displayed and it has no end. I'm not sure what triggered this can anyone help me with this I'm just starting to learn android.
Upvotes: 2
Views: 352
Reputation: 331
You need to clear the old content of the TextView before appending any new content
//hardware
hardwareStatus.setText("") // Clear old response data from TextView
for (int i = 0; i < response.body().getHardware().length; i++){
// Your logic
}
//software
softwareStatus.setText("") // Clear old response data from TextView
for (int i = 0; i < response.body().getSoftware().length; i++){
// Your logic
}
You're seeing the old results because your appending to the results TextView
hardwareStatus.append(spannable);
hardwareStatus.append("\n\n");
Just use
hardwareStatus.setText(spannable);
From my understanding you are running into an infinite loop of api calls that is triggered every 5 seconds, if that is the case It's because you are calling the postDelayed function again inside the runnable, if you're looking to call the endpoint just one time then remove this line
handler.postDelayed(runnable = new Runnable() {
public void run() {
//do your function;
getSystemObject();
handler.postDelayed(runnable, apiDelayed); // Remove this
}
}, apiDelayed);
Another thing to note here is that you should be initializing the views in the
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
hardwareName = view.findViewById(R.id.hardware_name);
hardwareStatus = view.findViewById(R.id.hardware_status);
softwareName = view.findViewById(R.id.software_name);
softwareStatus = view.findViewById(R.id.software_status);
flagName = view.findViewById(R.id.flagreport_name);
}
Also you're building the retrofit instance every time you call the api, it's better to create a singleton for retrofit and call the create method to initialize a global variable in your fragment or activity, then use this variable in your function like this
// Global variable
private WebApi api = Retrofit.getInstance().create(WebApi.class);
then in any function call
api.getNameStatus();
Upvotes: 1
Reputation: 8303
Call getSystemObject
from onViewCreated
instead of from onResume
.
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
handler.postDelayed(runnable = new Runnable() {
public void run() {
//do your function;
getSystemObject();
handler.postDelayed(runnable, apiDelayed);
}
}, apiDelayed);
}
Upvotes: 0