如何初始化片段中的綁定屬性以使雙向數據綁定工作

所以我在做一個項目,我完全迷路了。我已經看到了如何使用textview進行數據綁定,但是有人要求我使用EditText視圖進行雙向數據綁定。到目前為止,我都是帶著它來的。

The XML File.

<?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"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="myShoe"
            type="com.udacity.shoestore.product.Shoe" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/colorPrimary">

        <TextView
            android:id="@+id/title_detail_view"
            style="@style/title_style"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="80dp"
            android:text="@string/add_shoe_title"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <EditText
            android:id="@+id/shoe_name"
            style="@style/login_style"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:ems="10"
            android:hint="@string/shoe_name_string"
            android:inputType="text"
            android:textSize="30sp"
            android:text="@={myShoe.name}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title_detail_view" />

        <EditText
            android:id="@+id/shoe_size"
            style="@style/login_style"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/size_string"
            android:inputType="number|numberDecimal"
            android:textSize="15sp"
            android:text="@={myShoe.size}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/shoe_name" />

        <EditText
            android:id="@+id/company_name"
            style="@style/login_style"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/company_string"
            android:inputType="text"
            android:textSize="15sp"
            android:text="@={myShoe.company}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/shoe_size" />

        <EditText
            android:id="@+id/shoe_description"
            style="@style/login_style"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/description_string"
            android:inputType="text"
            android:textSize="15sp"
            android:text="@={myShoe.description}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/company_name" />

        <Button
            android:id="@+id/cancel_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:backgroundTint="@color/colorPrimaryDark"
            android:text="@string/cancel_string"
            android:textColor="@android:color/white"
            app:layout_constraintBaseline_toBaselineOf="@+id/savee_button"
            app:layout_constraintEnd_toStartOf="@+id/savee_button"
            app:layout_constraintStart_toStartOf="parent" />

        <Button
            android:id="@+id/savee_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="16dp"
            android:layout_marginEnd="88dp"
            android:backgroundTint="@color/colorPrimaryDark"
            android:text="@string/save_string"
            android:textColor="@android:color/white"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/shoe_description" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

我被告知要實現它的一個片段,它應該工作。但我不知道怎么做。這是碎片

class ShoeDetailsFragment : Fragment() {

    private val viewModel: ActivityViewModel by activityViewModels()

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {


        val binding: FragmentShoeDetailsBinding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_shoe_details,
            container, false
        )



        //initializing the button and clearing the views once canceled
        binding.cancelButton.setOnClickListener { v: View ->
            v.findNavController().navigateUp()

            binding.shoeName.text.clear()
            binding.shoeSize.text.clear()
            binding.companyName.text.clear()
            binding.shoeDescription.text.clear()

        }
        //initializing the button and saving the info to transfer to the shoeList
        binding.saveeButton.setOnClickListener { v: View ->
            v.findNavController().navigateUp()

            val name = shoe_name.text.toString()
            val size = shoe_size.text.toString()
            val brand = company_name.text.toString()
            val details = shoe_description.text.toString()
            viewModel.addShoe(name, size, brand, details)
        }

        return binding.root
    }
}

我愿意接受任何初始化綁定屬性的想法,這樣我就可以在布局和片段中使用它。還是我看錯了?

另外,XML文件在這個片段中表示

? 最佳回答:

我想我也為我的納米學位做了這個項目,你的代碼給我留下了深刻的印象。

在我的ViewModel中,我為每個EditText創建了3個變量

  • MutableLiveData—更新viewModel內的值
  • LiveData將值暴露在viewModel之外,例如在片段中(您實際上不需要這個)
  • 公共變量來監視MutableLiveData的值,并將其公開給xml,從而實現雙向綁定。

然后我和將創建一個共享ViewModel,在ShoeDetailsFragmentShoeListingFragment之間共享數據。

在SharedViewModel中

我為每個EditText創建了3個變量(這只是前2個EditText)

class MySharedViewModel : ViewModel() {
 private val _name = MutableLiveData<String>()
    val name: LiveData<String>
        get() = _name
    var edShoeName = ""

    private val _size = MutableLiveData<Double>()
    val size: LiveData<Double>
        get() = _size
    var edSize = ""

......}

對于xml,我做了與您所做的完全相同的操作,但是使用了第3個變量作為雙向數據綁定

<EditText
            android:id="@+id/shoe_name"
            style="@style/login_style"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginStart="16dp"
            android:layout_marginEnd="16dp"
            android:ems="10"
            android:hint="@string/shoe_name_string"
            android:inputType="text"
            android:textSize="30sp"
            android:text="@={mySharedViewModel.edShoeName}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/title_detail_view" />

        <EditText
            android:id="@+id/shoe_size"
            style="@style/login_style"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:hint="@string/size_string"
            android:inputType="number|numberDecimal"
            android:textSize="15sp"
            android:text="@={mySharedViewModel.edCompany}"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/shoe_name" />

我看到您在ShoeDetailFragment代碼中包含了這行代碼

 binding.saveeButton.setOnClickListener { v: View -> ....}

在我的例子中,我是在SharedViewModel中做的。

fun onSaveButtonClick() {

        //check value entered for size and set it to 0 if blank
        if (edSize == "") {

            edSize = "0"
        }

       

        //update MutableLiveData with values read live from the EditText
        _name.value = edShoeName
        _size.value = edSize.toDouble()
        

        //save shoeObject to the _shoeList MutableLiveData
        _shoeList.value?.add(Shoe(edShoeName, edSize.toDouble(), edCompany, edDescription))

    }

使用數據綁定,我將onClick位移動到xml

<Button
        android:id="@+id/buttonSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="75dp"
        android:layout_marginBottom="75dp"
        android:background="@drawable/custom_button_background"
        android:onClick="@{()->sharedViewModel.onSaveButtonClick()}"

所有與你的項目最好的,你也可以參考我的項目,如果需要的話

快樂的編碼伙伴

主站蜘蛛池模板: 一区二区三区视频在线观看| 亚洲AV无码一区二区三区网址| 在线观看日本亚洲一区| 北岛玲在线一区二区| 中文字幕乱码一区久久麻豆樱花| 色婷婷AV一区二区三区浪潮| 国产精品第一区揄拍无码| 日韩一区精品视频一区二区| 中文字幕VA一区二区三区 | 国产伦精品一区二区三区免费迷 | 亚洲熟女乱色一区二区三区 | 亚洲AⅤ无码一区二区三区在线| 另类一区二区三区| 国产亚洲综合一区柠檬导航| 日韩精品无码一区二区三区AV| 国产成人无码精品一区不卡| 内射少妇一区27P| 日韩一区二区三区免费播放| 日韩视频在线一区| 国产福利在线观看一区二区 | 日韩精品一区二区三区中文| 99久久精品国产免看国产一区| 国产日韩高清一区二区三区| 无人码一区二区三区视频| 99偷拍视频精品一区二区| 一区二区不卡视频在线观看| 亚洲AV噜噜一区二区三区| 一区二区三区在线观看| 人妻精品无码一区二区三区 | 一本AV高清一区二区三区| 日本大香伊一区二区三区| 国产熟女一区二区三区四区五区| 韩国福利一区二区三区高清视频 | 日韩一区二区a片免费观看| 亚洲av无码一区二区三区在线播放 | 在线一区二区观看| 国产精品电影一区二区三区| 武侠古典一区二区三区中文| AV天堂午夜精品一区 | 一区二区国产在线观看| 久久无码一区二区三区少妇|