Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Extracting data from multiple EditTexts without code repetition

Posted on November 14, 2022

Solution 1 :

The general ways for not repeating code are usually loops and functions. In this case you should add your EditTexts to an array like data structure and loop over them.

For example:

You could store the edit Texts in a Hash map and then use a loop to add them to the form:

new HashMap<EditText, String>() {{
    put(R.id.et1, "name");
    put(R.id.et2, "phone");
    //etc...
}}.forEach((editText, s) -> formData.put(s, editText.getText().toString()));

If you don’t mind using uninformative names you can just use an array:

EditText[] toAdd = new EditText[]{findViewById(R.id.et1), findViewById(R.id.et2)};
for (int i = 0; i < toAdd.length; i++) {
    formData.put("data" + i, toAdd[i].getText().toString());
}

Or for a nice mix of both, you can use the ID of the EditText as the name

EditText[] toAdd = new EditText[]{findViewById(R.id.et1), findViewById(R.id.et2)};
for (EditText editText : toAdd) {
    formData.put(getResources().getResourceEntryName(editText.getId()), editText.getText().toString());
}

Problem :

I’d like to efficiently extract data from the EditText fields in a form, without having to repeat the same code multiple times. Here’s my current implementation for four EditTexts:

    EditText editText1 = (EditText) findViewById(R.id.editText1);
    String message1 = editText1.getText().toString();
    formData.put("data1", message1);

    EditText editText2 = (EditText) findViewById(R.id.editText2);
    String message2 = editText2.getText().toString();
    formData.put("data2", message2);

    EditText editText3 = (EditText) findViewById(R.id.editText3);
    String message3 = editText3.getText().toString();
    formData.put("data3", message3);

    EditText editText4 = (EditText) findViewById(R.id.editText4);
    String message4 = editText4.getText().toString();
    formData.put("data4", message4);
READ  [ANSWERED] java - Object getting faster, without any reason
Powered by Inline Related Posts

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress