Solution 1 :
You should update Edittext
inside when your api get done. Try this
if(!summaryDataResponse.getError()){
temperature = Math.round(summaryDataResponse.getSummary().getTMP() * 100.0) / 100.0;
pressure = Math.round(summaryDataResponse.getSummary().getPRE() * 100.0) / 100.0;
humidity = Math.round(summaryDataResponse.getSummary().getHR() * 100.0) / 100.0;
if (!isRemoving()) {
editTextTemperature.setText(((temperature != null)) ? String.valueOf(temperature) : "NaN") + " ºC");
editTextPressure.setText(((pressure != null) ? String.valueOf(pressure) : "NaN") + " hPa");
editTextHumidity.setText(((humidity != null)
}
}
Problem :
I’m trying to read meteorological values like temperature, pressure… and I have some EditText in the main fragment layout, that opens immediatly when the application is opened.
The point is that I wanna call using Retrofit2 my API developed with Slim PHP, to read these values and then set the EditText texts with this values.
These are the onCreate and onCreateView methods I have in the fragment class:
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
this.readSummaryData();
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_patient_index, container, false);
editTextTemperature = v.findViewById(R.id.teperaturaTxt_patientMain);
editTextPressure = v.findViewById(R.id.pressureTxt_patientMain);
editTextHumidity = v.findViewById(R.id.humidityTxt_patientMain);
editTextTemperature.setText(((temperature != null)) ? String.valueOf(temperature) : "NaN") + " ºC");
editTextPressure.setText(((pressure != null) ? String.valueOf(pressure) : "NaN") + " hPa");
editTextHumidity.setText(((humidity != null) ? String.valueOf(humidity) : "NaN") + " %");
return v;
}
And this is the readSummaryData method:
private void readSummaryData(){
Call<ReadSummaryDataResponse> readSummaryData = RetrofitClient.getInstance().getAPI().readSummaryData();
readSummaryData.enqueue(new Callback<ReadSummaryDataResponse>() {
@Override
public void onResponse(Call<ReadSummaryDataResponse> call, Response<ReadSummaryDataResponse> response) {
try {
ReadSummaryDataResponse summaryDataResponse = response.body();
if(!summaryDataResponse.getError()){
temperature = Math.round(summaryDataResponse.getSummary().getTMP() * 100.0) / 100.0;
pressure = Math.round(summaryDataResponse.getSummary().getPRE() * 100.0) / 100.0;
humidity = Math.round(summaryDataResponse.getSummary().getHR() * 100.0) / 100.0;
}
else{
Toast.makeText(getActivity(), "ERROR", Toast.LENGTH_SHORT).show();
}
}
catch (Exception e){
Log.e("Exception", e.getMessage());
}
}
@Override
public void onFailure(Call<ReadSummaryDataResponse> call, Throwable t) {
}
});
}
The summaryData response contains the summary class which has the temperature, pressure and humidity values.
So when I open this fragment when starting the application the values of the EditText are all “NaN”, but if I go to another fragment, and then come back to the main fragment the values aren’t “NaN”.
I think this may be because the EditText.setText(String) is done before the readSummaryData, and I don’t know how to solve it.
Thanks everyone
Comments
Comment posted by Dozer
Yes, now it’s working, thanks. But what is the isRemoving()?
Comment posted by Công Hải
because when fragment remove or destroy your API still call. It means API done after that so you have to check to ensure only update UI if need