Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Android Retrofit2: How to read and set EditText values when fragment is created?

Posted on November 11, 2022 By

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”.

READ  [FIXED] android - newly created Chromebook OS emulator crashes on start up
Powered by Inline Related Posts

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

Android Tags:android, android-fragments, java, retrofit2

Post navigation

Previous Post: [FIXED] java – How can I solve this Android Studio Firebase problem?
Next Post: [FIXED] Android Java Publish result cast issue at MyCustomAdapter.java

Related Posts

[FIXED] android – Will creating a Toast inside a Runnable cause a memory leak? Android
[FIXED] ANDROID_SDK_ROOT=undefined (recommended setting) while building ionic app using Cordova for Android device Android
[FIXED] android – Calling js method onCreateView failed System.err: TypeError: Right-hand side of ‘instanceof’ is not an object Nativescript Android
[FIXED] java – android studio app keeps stopping when posting json Android
[FIXED] android – RecyclerView items inside BottomSheetDialogFragment need double touch after fast scrolling Android
[FIXED] kotlin – Jetpack Compose: TextField aligns outside of AlertDialog as text grows Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme