在Kotlin中获取选中单选按钮值并写入Room数据库的方法
解决RadioGroup优先级存入Room的实现方案
1. 完善RadioButton布局配置
给每个RadioButton添加android:tag属性,直接对应要存入数据库的字符串值,方便后续获取:
<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" android:tag="high" android:text="高优先级"/> <RadioButton android:id="@+id/AddNoteFragmentMediumPriorityRadioBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:tag="medium" android:text="中优先级"/> <RadioButton android:id="@+id/AddNoteFragmentLowPriorityRadioBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margin="8dp" android:tag="low" android:text="低优先级"/> </RadioGroup>
2. 在AddNoteFragment中获取选中的优先级字符串
在按钮点击回调里,通过RadioGroup获取选中的RadioButton,取出对应的tag作为优先级字符串:
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{ // 获取选中的优先级 val selectedRadioBtnId = binding.AddNoteFragmentRadioGroup.checkedRadioButtonId val priority = if (selectedRadioBtnId != -1) { val selectedBtn = view.findViewById<RadioButton>(selectedRadioBtnId) selectedBtn.tag.toString() } else { // 未选中时设置默认优先级 "low" } viewModel.makeNote( binding.AddNoteFragmentTxtTitle.text.toString(), binding.AddNoteFragmentTxtNote.text.toString(), currentDate, priority // 传入优先级字符串 ) } }
3. 修改AddNoteViewModel的方法,添加优先级参数
更新makeNote方法,接收优先级参数,并创建包含优先级的Note对象:
@HiltViewModel class AddNoteViewModel @Inject constructor( private val repository: INoteRepository, ): ViewModel() { private fun insertNote(note: Note) = viewModelScope.launch{ repository.Insert(note) } fun makeNote(title:String, noteContent:String, currentDate:String, priority:String){ if(title.isEmpty() || noteContent.isEmpty()){ println("Enter title, note content, priority") return } // 确保Note实体类包含priority参数 val note = Note(title = title, content = noteContent, date = currentDate, priority = priority) insertNote(note) } }
4. 确认Note实体类的结构
确保Room实体类Note包含priority字段,示例如下:
@Entity(tableName = "notes") data class Note( @PrimaryKey(autoGenerate = true) val id: Int = 0, val title: String, val content: String, val date: String, val priority: String // 新增的优先级字段 )
额外注意事项
- 如果之前已创建数据库表,添加
priority字段后需更新Room版本号,可根据需求设置迁移策略,测试阶段可暂时用fallbackToDestructiveMigration = true。 - 可增加优先级选中校验,比如未选中时弹窗提示用户选择,替代直接设置默认值。
内容的提问来源于stack exchange,提问作者NewPartizal
相关产品推荐
相关产品推荐

