EditText.getText (). ToString () не возвращает текущее значение - PullRequest
0 голосов
/ 22 марта 2020

Я пытаюсь получить текст, который был вставлен в EditText в пользовательском интерфейсе.

Интерфейс выглядит следующим образом. Фрагмент EditText для имени имеет значение по умолчанию «Hello».

enter image description here

После того, как пользователь ввел новое значение (например, «Hello2»), я хотел бы получить новое значение, когда пользователь нажимает на Кнопка добавления.

enter image description here

Но то, что я получаю, остается значением по умолчанию "Hello".

Мой код выглядит следующим образом:

XML

<TextView
    android:id="@+id/textView_name"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/backgroundtint_color"
    android:text="Name"
    android:textColor="@color/ntext_foreground"
    android:textSize="@dimen/ntext_fontsize"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.075"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@id/textview_addingredient"
    app:layout_constraintVertical_bias="0.050" />

JAVA Код во фрагменте

public class AddIngredient extends Fragment{

//Controls
public EditText etN;
public EditText etK;
public EditText etF;
public EditText etC;
public EditText etP;
public TextView tv1;

//ViewModel
AddIngredientViewModel model;

@Override
public View onCreateView(
        LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState
) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_addingredient, container, false);

    // Get the values
    tv1 = rootView.findViewById(R.id.textview_addingredient);
    etN = rootView.findViewById(R.id.editText_name);
    etK = rootView.findViewById(R.id.editText_kcal);
    etF = rootView.findViewById(R.id.editText_fat);
    etC = rootView.findViewById(R.id.editText_carbs);
    etP = rootView.findViewById(R.id.editText_protein);

    // Inflate the layout for this fragment
    return rootView;
}


public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    view.findViewById(R.id.fragmentbutton_addingredient).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            //Get current values
            String name = etN.getText().toString();
            int kcal = Integer.parseInt(etK.getText().toString().trim());
            int fat = Integer.parseInt(etF.getText().toString().trim());
            int carbs = Integer.parseInt(etC.getText().toString().trim());
            int protein = Integer.parseInt(etP.getText().toString().trim());

            model = new ViewModelProvider(requireActivity()).get(AddIngredientViewModel .class);

            Ingredient ingredient = new Ingredient(name, kcal, fat, carbs, protein){};
            model.SaveIngredient(ingredient);
        }
    });
}

}

Так есть у кого-нибудь идея, что я могу изменить, чтобы получить текущее значение? Спасибо!

- РЕДАКТИРОВАТЬ -

Весь XML:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/screen_background"
    tools:context=".fragments.AddIngredient">

    <TextView
        android:id="@+id/textview_addingredient"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="68dp"
        android:text="Add Ingredient"
        android:textColor="@color/headercolor"
        android:textSize="@dimen/header_fontsize"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.126"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        />

    <!-- Name -->
    <TextView
        android:id="@+id/textView_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Name"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textview_addingredient"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="textPersonName"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        android:text="Hello"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_name"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Kcal -->
    <TextView
        android:id="@+id/textView_kcal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Kcal"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_name"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_kcal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_kcal"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Fat -->
    <TextView
        android:id="@+id/textView_fat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Fat"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_kcal"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_fat"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_fat"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Carbs -->
    <TextView
        android:id="@+id/textView_carbs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Carbs"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.075"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_fat"
        app:layout_constraintVertical_bias="0.050" />

    <EditText
        android:id="@+id/editText_carbs"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_carbs"
        app:layout_constraintVertical_bias="0.0" />

    <!-- Protein -->
    <TextView
        android:id="@+id/textView_protein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:text="Protein"
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.078"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_carbs"
        app:layout_constraintVertical_bias="0.052" />

    <EditText
        android:id="@+id/editText_protein"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:backgroundTint="@color/backgroundtint_color"
        android:ems="10"
        android:inputType="number"
        android:text=""
        android:textColor="@color/ntext_foreground"
        android:textSize="@dimen/ntext_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.157"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/textView_protein"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/fragmentbutton_addingredient"
        android:layout_width="@dimen/button_width"
        android:layout_height="@dimen/button_height"
        android:background="@color/button_background"
        android:gravity="left|center_vertical"
        android:padding="20dp"
        android:text="Add"
        android:textColor="@color/button_foreground"
        android:textSize="@dimen/button_fontsize"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.243"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/editText_protein"
        app:layout_constraintVertical_bias="0.250" />

</androidx.constraintlayout.widget.ConstraintLayout>

1 Ответ

1 голос
/ 22 марта 2020

Удалите текст stati c по умолчанию, установленный в поле editTextName в файле макета xml. Из-за этого значение всегда сохраняется как Hello. Вы не должны устанавливать текст в EditTextview.

  <EditText
         android:id="@+id/editText_name"
         android:layout_width="wrap_content"
        android:layout_height="wrap_content"

  android:backgroundTint="@color/backgroundtint_color"
          android:ems="10"
         android:inputType="textPersonName"
         android:textColor="@color/ntext_foreground"
         android:textSize="@dimen/ntext_fontsize"
         android:text="Hello"  //Remove this line.
...