简体   繁体   English

Livedata 与 jetpack 组合

[英]Livedata in combination with jetpack compose

I'm trying to learn jetpack compose and tried to make an easy app where you can add restaurants together with a score in an overview list using room.我正在尝试学习jetpack compose,并尝试制作一个简单的应用程序,您可以在其中使用房间在概览列表中添加餐厅和分数。

However, everytime I try to type a word in the textfield, the textfield is not updated and remains empty.但是,每次我尝试在文本字段中键入一个单词时,文本字段都不会更新并且保持为空。 I would expect that updating the locationName in the onValueChange function would trigger an update of the state as it is part of a LiveData class.我希望更新 onValueChange function 中的 locationName 会触发 state 的更新,因为它是 LiveData class 的一部分。 How can I solve this without having to make a seperate UI model in my modelView and having it converted to my entity object in case the data needs to be persisted?如何解决这个问题而不必在我的模型视图中创建一个单独的 UI model 并将其转换为我的实体 object 以防数据需要被持久化?

My compose function我的作曲 function


@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))) })

            }

    }
}

My viewmodel我的视图模型

class AddItemViewModel () : ViewModel() {
    var location : LiveData<LocationEntity> = MutableLiveData(LocationEntity())

}

My data model我的数据 model

    @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
        }

    }

With latest compose version (1.0.0-alpha09), for TextField section, you can do something like:使用最新的撰写版本(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}")
}

Here, instead of saving data while user is typing, data will be saved when user has performed an action.在这里,不是在用户键入时保存数据,而是在用户执行操作时保存数据。

Don't use Modifier.Companion, use Modifier only.不要使用 Modifier.Companion,仅使用 Modifier。

Don't use vertical scroller, use lazycolumn不要使用垂直滚动条,使用lazycolumn

Try this in your viewmodel在你的视图模型中试试这个

var _location = MutableLiveData(LocationEntity())
var location: LiveData<LocationEntity>
get() = _location

fun onLocationChange(data){
_locations.value!!. locationName = data //Confirm that u r doing it right.
}

Are you sure your room implementation is correct?你确定你的房间实施是正确的吗? No DAO, no repository?没有 DAO,没有存储库?

Anyway try this out and let me know无论如何试试这个,让我知道

The answer of Habib Kazemi in the comments was the correct one and resolved the issue: 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) location.value...locationName = data 更新值无济于事,您应该调用 addItemViewNodel.location.setValue(new Instance of LocationEntity with desired locationName)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM