簡體   English   中英

無法使用 TextInputLayout 更新 Firestore 中的數據

[英]Can't update data in Firestore with TextInputLayout

我在編輯配置文件屏幕上使用 TextInputLayout,我想更新該屏幕中的數據,但是當我使用它時,數據更新為喜歡

“com.google.android.material.textfield.TextInputLayout{9544eb3 V.ED..... ....... 93,232-931,455 #7f0a00d8 app:id/etEditName aid=1073741880}”

我會留下我的代碼。

ProfileFragment.kt

....
    
    binding.btnEditProfile.setOnClickListener {
            val dialog =
                LayoutInflater.from(context).inflate(R.layout.edit_profile_dialog, null)
            val builder = AlertDialog.Builder(context).setView(dialog).show()
            builder.window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
            builder.setCancelable(true)

            val etName = dialog.findViewById<TextInputLayout>(R.id.etEditName)
            etName.editText?.setText(user.name)
            val etSurname = dialog.findViewById<TextInputLayout>(R.id.etEditSurname)
            etSurname.editText?.setText(user.surname)
            val etWeight = dialog.findViewById<TextInputLayout>(R.id.etEditWeight)
            etWeight.editText?.setText(user.weight)
            val etHeight = dialog.findViewById<TextInputLayout>(R.id.etEditHeight)
            etHeight.editText?.setText(user.height)
            val etGoal = dialog.findViewById<TextInputLayout>(R.id.etEditGoal)
            etGoal.editText?.setText(user.calorieGoal.toString())

            dialog.findViewById<Button>(R.id.btnEditProfile).setOnClickListener {
                if (etSurname.isNotEmpty() && etHeight.isNotEmpty() && etWeight.isNotEmpty() && etGoal.isNotEmpty())
                    dbUser.document(auth.currentUser?.email.toString())
                        .update(
                            mapOf(
                                "name" to etName.toString(),
                                "surname" to etSurname.toString(),
                                "height" to etHeight.toString(),
                                "weight" to etWeight.toString(),
                            )
                        )
                        .addOnSuccessListener {
                            Toast.makeText(context, "Updated!!", Toast.LENGTH_SHORT).show()
                            builder.dismiss()
                        } else {
                    Toast.makeText(context, "Fill in the fields!!", Toast.LENGTH_SHORT).show()
                }
            }
        }

edit_dialog.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginHorizontal="36dp"
    android:background="@drawable/dialog_bg">

    <TextView
        android:id="@+id/txtEditProfile"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:fontFamily="monospace"
        android:text="@string/edit_profile"
        android:textColor="@color/primaryDarkColor"
        android:textSize="32sp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditName"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/enter_name"
        android:maxLines="1"
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/txtEditProfile">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPersonName"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditSurname"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/enter_surname"
        android:maxLines="1"
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditName">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:inputType="textPersonName"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditWeight"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:focusableInTouchMode="false"
        android:hint="@string/weight"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:layout_constraintEnd_toStartOf="@+id/etEditHeight"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditSurname">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditGoal"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="140dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="8dp"
        android:hint="@string/calorie_goal"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toStartOf="@+id/etEditHeight"
        app:layout_constraintHorizontal_bias="0.508"
        app:layout_constraintStart_toEndOf="@+id/etEditWeight"
        app:layout_constraintTop_toBottomOf="@id/etEditHeight">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/etEditHeight"
        style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
        android:layout_width="110dp"
        android:layout_height="wrap_content"
        android:layout_marginHorizontal="24dp"
        android:layout_marginTop="24dp"
        android:hint="@string/height"
        android:maxLines="1"
        android:text=""
        app:errorEnabled="true"
        app:hintTextColor="@color/gray"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/etEditWeight"
        app:layout_constraintTop_toBottomOf="@id/etEditSurname">

        <com.google.android.material.textfield.TextInputEditText
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:cursorVisible="false"
            android:inputType="number"
            android:textColorHint="@color/primaryDarkColor" />
    </com.google.android.material.textfield.TextInputLayout>


    <Button
        android:id="@+id/btnEditProfile"
        android:layout_width="250dp"
        android:layout_height="wrap_content"
        android:layout_margin="24dp"
        android:text="@string/submit"
        android:textColor="@color/secondaryTextColor"
        android:textStyle="bold"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/etEditGoal" />

</androidx.constraintlayout.widget.ConstraintLayout>

感謝您的幫助:)

當您需要更新 TextInputLayout 內的 EditText 中的文本時,您正在更新 TextInputLayout 本身。 所以更換

"name" to etName.toString(),
"surname" to etSurname.toString(),
"height" to etHeight.toString(),
"weight" to etWeight.toString(),

"name" to etName.editText?.text.toString(),
"surname" to etSurname.editText?.text.toString(),
"height" to etHeight.editText?.text.toString(),
"weight" to etWeight.editText?.text.toString(),

這將更新“編輯配置文件”屏幕中的用戶數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM