【发布时间】:2022-09-22 22:20:33
【问题描述】:
我正在尝试制作一个待办事项应用程序,并将单选组设置为具有高、中和低优先级的单选按钮。但是,每次都出现不同的错误。我正在尝试根据这个点击的单选按钮将优先级以字符串的形式打印到房间数据库,例如,如果用户点击高优先级单选按钮,它将在房间数据库的优先级列中写入高。我怎样才能做到这一点。
添加笔记片段
private var fragmentAddNoteBinding:FragmentAddNoteBinding? = null
private lateinit var viewModel:AddNoteViewModel
private val sdf = SimpleDateFormat(\"yyyy-MM-dd\")
private val currentDate = sdf.format(Date())
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel = ViewModelProvider(requireActivity()).get(AddNoteViewModel::class.java)
val binding =FragmentAddNoteBinding.bind(view)
fragmentAddNoteBinding = binding
binding.AddNoteFragmentBtn.setOnClickListener{
viewModel.makeNote(
binding.AddNoteFragmentTxtTitle.text.toString(),
binding.AddNoteFragmentTxtNote.text.toString(),
currentDate
// priority <-- here I want to send priority as string like this
)
}
}
添加NoteViewModel
@HiltViewModel
class AddNoteViewModel @Inject constructor(
private val repository: INoteRepository,
): ViewModel() {
private fun insertNote(note: Note) = viewModelScope.launch{
repository.Insert(note)
}
fun makeNote(title:String,note:String,currentDate:String){ //here I will take the priority as a parameter and create a new note and save it to room
if(title.isEmpty() || note.isEmpty()){
println(\"Enter titel,note,priority\")
return
}
val note = Note(note,title,currentDate)
insertNote(note)
}
}
我的广播组 xml
<RadioGroup
android:id=\"@+id/AddNoteFragmentRadioGroup\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_margin=\"8dp\"
android:orientation=\"horizontal\">
<RadioButton
android:id=\"@+id/AddNoteFragmentHighPriorityRadioBtn\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_margin=\"8dp\"></RadioButton>
<RadioButton
android:id=\"@+id/AddNoteFragmentMediumPriorityRadioBtn\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_margin=\"8dp\"></RadioButton>
<RadioButton
android:id=\"@+id/AddNoteFragmentLowPriorityRadioBtn\"
android:layout_width=\"wrap_content\"
android:layout_height=\"wrap_content\"
android:layout_margin=\"8dp\"></RadioButton>
</RadioGroup>