Livedata 结合 Jetpack Compose
Livedata in combination with jetpack compose
我正在尝试学习 Jetpack Compose 并尝试制作一个简单的应用程序,您可以在其中使用房间在概览列表中添加餐厅和分数。
但是,每次我尝试在文本字段中键入一个词时,文本字段都没有更新并且仍然是空的。我希望在 onValueChange 函数中更新 locationName 会触发状态更新,因为它是 LiveData class 的一部分。我怎样才能解决这个问题,而不必在我的模型视图中创建一个单独的 UI 模型并将其转换为我的实体对象以防数据需要保留?
我的撰写功能
@Composable
fun AddItemScreenBody(modifier: Modifier = Modifier.Companion) {
VerticalScroller {
Column(modifier) {
var locations = addItemViewNodel.location.observeAsState()
FilledTextField(
value = locations.value!!.locationName,
onValueChange = { data -> locations.value!!.locationName = data },
label = { Text(stringResource(R.string.name), style = TextStyle(color = Color(0xFFC7C7C7))) })
}
}
}
我的视图模型
class AddItemViewModel () : ViewModel() {
var location : LiveData<LocationEntity> = MutableLiveData(LocationEntity())
}
我的数据模型
@Entity(tableName = "locations")
class LocationEntity @Ignore constructor () {
@PrimaryKey(autoGenerate = true)
var id: Long = 0
@ColumnInfo(name = "location_name")
var locationName: String = ""
constructor (id: Long, locationName: String, category: Category) : this()
{
this.id = id
this.locationName = locationName
}
}
使用最新的 compose 版本 (1.0.0-alpha09),对于 TextField
部分,您可以执行以下操作:
@Composable
fun MyTextField() {
Column(Modifier.padding(16.dp)) {
val textFieldState = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textFieldState.value = it },
onImeActionPerformed = { imeAction,controller ->
if (imeAction == ImeAction.Done){
locations.value.locationName = textFieldState.value.text
controller?.hideSoftwareKeyboard()
}
}
)
Text("text field value is: ${textFieldState.value.text}")
}
在这里,不是在用户键入时保存数据,而是在用户执行操作时保存数据。
不要使用 Modifier.Companion,仅使用修饰符。
不要使用垂直滚动条,使用 lazycolumn
在您的视图模型中试试这个
var _location = MutableLiveData(LocationEntity())
var location: LiveData<LocationEntity>
get() = _location
fun onLocationChange(data){
_locations.value!!. locationName = data //Confirm that u r doing it right.
}
您确定您的房间实施正确吗?没有 DAO,就没有存储库?
无论如何都试试这个并告诉我
Habib Kazemi 在评论中的回答是正确的并解决了问题:
locations.value!!.locationName = data updating the value won't help
instead you should call addItemViewNodel.location.setValue(new
Instance of LocationEntity with desired locationName)
我正在尝试学习 Jetpack Compose 并尝试制作一个简单的应用程序,您可以在其中使用房间在概览列表中添加餐厅和分数。
但是,每次我尝试在文本字段中键入一个词时,文本字段都没有更新并且仍然是空的。我希望在 onValueChange 函数中更新 locationName 会触发状态更新,因为它是 LiveData class 的一部分。我怎样才能解决这个问题,而不必在我的模型视图中创建一个单独的 UI 模型并将其转换为我的实体对象以防数据需要保留?
我的撰写功能
@Composable
fun AddItemScreenBody(modifier: Modifier = Modifier.Companion) {
VerticalScroller {
Column(modifier) {
var locations = addItemViewNodel.location.observeAsState()
FilledTextField(
value = locations.value!!.locationName,
onValueChange = { data -> locations.value!!.locationName = data },
label = { Text(stringResource(R.string.name), style = TextStyle(color = Color(0xFFC7C7C7))) })
}
}
}
我的视图模型
class AddItemViewModel () : ViewModel() {
var location : LiveData<LocationEntity> = MutableLiveData(LocationEntity())
}
我的数据模型
@Entity(tableName = "locations")
class LocationEntity @Ignore constructor () {
@PrimaryKey(autoGenerate = true)
var id: Long = 0
@ColumnInfo(name = "location_name")
var locationName: String = ""
constructor (id: Long, locationName: String, category: Category) : this()
{
this.id = id
this.locationName = locationName
}
}
使用最新的 compose 版本 (1.0.0-alpha09),对于 TextField
部分,您可以执行以下操作:
@Composable
fun MyTextField() {
Column(Modifier.padding(16.dp)) {
val textFieldState = remember { mutableStateOf(TextFieldValue()) }
TextField(
value = textState.value,
onValueChange = { textFieldState.value = it },
onImeActionPerformed = { imeAction,controller ->
if (imeAction == ImeAction.Done){
locations.value.locationName = textFieldState.value.text
controller?.hideSoftwareKeyboard()
}
}
)
Text("text field value is: ${textFieldState.value.text}")
}
在这里,不是在用户键入时保存数据,而是在用户执行操作时保存数据。
不要使用 Modifier.Companion,仅使用修饰符。
不要使用垂直滚动条,使用 lazycolumn
在您的视图模型中试试这个
var _location = MutableLiveData(LocationEntity())
var location: LiveData<LocationEntity>
get() = _location
fun onLocationChange(data){
_locations.value!!. locationName = data //Confirm that u r doing it right.
}
您确定您的房间实施正确吗?没有 DAO,就没有存储库?
无论如何都试试这个并告诉我
Habib Kazemi 在评论中的回答是正确的并解决了问题:
locations.value!!.locationName = data updating the value won't help instead you should call addItemViewNodel.location.setValue(new Instance of LocationEntity with desired locationName)