当时我正在阅读与LiveData和ViewModels有关的安卓文档,我遇到了一条令我困惑的条目。
在中,示例代码实现如下所示的观察者
public class NameActivity extends AppCompatActivity {
private NameViewModel model;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Other code to setup the activity...
例如,假设我们有一个产品目录视图,其中包含将产品添加到购物车的选项。每次用户单击add to cart时,都会调用一个viewModel方法addToCart,它可能如下所示:
//inside viewModel
fun addToCart(item:Item): LiveData<Result> = liveData {
val result = repository.addToCart(item) // loadUser is a suspend function.
emit(result)
}
//inside view
addButton.onClic
这是我的CartProductItemDao。
@Dao
public interface CartProductItemDao {
@Query("select * from CartProductItem where product__category__id = :id")
LiveData<List<CartProductItem>> getCartProductItemList(int id);
@Query("select * from CartProductItem where product__
这是我的伪代码。 class ViewModel {
val liveData1: MutableLiveData<String>
private set
val liveData2: MutableLiveData<String>
private set
fun start() {
liveData1.value = "render1"
liveData2.value = "render2"
/**
* I w
今天我有一些关于android上的mvvm和数据绑定的问题,
我正在尝试绑定视图上的对象属性。
我有一个对象(Model),它有一些属性,例如:
public String name;
public String title;
public int value;
我有一个带有livedata的ViewModel,像这样:
MutableLiveData<Object> _obj = new MutableLiveData<>();
public LiveData<Object> obj = _obj;
最后,我有一个这样的观点:
<layout>
我从android库中注意到了一些奇怪的行为(至少在我看来很奇怪)。
虽然还有其他与类似的问题的帖子,但没有一个(我可以找到)与此完全相同。
我的问题是,当我观察一个LiveData对象时,我的观察者的onChange()方法被多次调用,但只对INSERT操作调用。对于我插入的每个对象,该方法将被触发一次(也就是说,如果我插入N个对象的列表,它将被触发N次),而对于其他操作(例如删除),不管我要删除的列表的大小如何,它都会被触发一次。
我的DAO中有以下查询:
@query("select * from myTable")
LiveData<myEntity> ge
在学习新的安卓架构组件的ViewModel和LiveData时,当观察到数据库源更改中的LiveData变化时,有一点困惑,以及这如何与光标适配器一起工作。
在中,它说
int FLAG_REGISTER_CONTENT_OBSERVER
If set the adapter will register a content observer on the cursor
and will call onContentChanged() when a notification comes in. Be
careful when using this flag: you will need
我是安卓系统中的新手,我正在开发一个应用程序,它可以从服务器上检索数据,在kotlin使用retrofit、kodein和MvvM,我在我的应用程序中设置了一个导航抽屉,目的是当我单击导航抽屉的条目时,新的活动打开,在这个活动中,我想显示回收视图,但是当新的活动打开时,不能在回收器上设置listitem,我在存储库和视图模型类中调试我的代码,我看到他们收到了数据,我在新的活动中调试了我的代码,我看到模型不能接收这些数据,并且在我的viewmodel.observe代码旁边设置无效的图标。
这是我的存储库类:
fun getdigitalproduct(): LiveData<List&
使用Android更新视图的“正确”方式似乎是LiveData。但我不能确定将其与模型联系起来的“适当”方式。我见过的大多数文档都显示了连接到Room,它返回一个LiveData对象。但是(假设我没有使用房间),在我的模型中返回一个LiveData对象(它是“生命周期感知的”,所以特定于安卓的活动/视图框架)对我来说似乎违反了关注点分离?
下面是一个包含Activity的示例。
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
su
有两种方法可以使MutableLiveData的更改值。但是setValue()和postValue()在MutableLiveData中的区别是什么?
我找不到同样的文件。
这是安卓的MutableLiveData类。
package android.arch.lifecycle;
/**
* {@link LiveData} which publicly exposes {@link #setValue(T)} and {@link #postValue(T)} method.
*
* @param <T> The type of data hold by this i
我像这样测试LiveData。
// MainActivity.kt
class MainActivity : AppCompatActivity() {
val testViewModel: TestViewModel by viewModels()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
test
我在我的应用上使用MVVM作为架构,但我在观察者代码中看到了一个意想不到的问题:观察者内部总是在获得真正的值之前输入一次。
myViewModel.getUserInfo().observe(this, androidx.lifecycle.Observer { user ->
if (user!= null) {
} else {
//THE FIRST TIME THROW HERE
}
在我的viewModel中,我有以下内容:
class MyViewModel : ViewModel() {
f
我发现,每次在DB中更新行时,Dao返回的LiveData都会调用它的观察者,即使LiveData值显然没有被更改。
考虑一个类似于以下示例的情况:
示例实体
@Entity
public class User {
public long id;
public String name;
// example for other variables
public Date lastActiveDateTime;
}
示例Dao
@Dao
public interface UserDao {
// I am only interested in the us
getMoreData()在ViewModel中是从ViewModel外部调用的,每次用户滚动到RecyclerView的底部。
存储库中的fetchMore()返回带有LoadingStatus对象的LiveData,该对象包含加载/成功/失败和错误消息
如何在loadingStatus中设置ViewModel变量,以便片段能够正确地观察它?
注意:当用户向下滚动时,可以多次调用ViewModel中的ViewModel。
ViewModel{
val loadingStatus
fun getMoreData(){
repository.fetchMore(
我对返回LiveData的房间有问题。
我创建带有函数的Dao来返回数据列表。我应该以LiveData的身份返回。但是,它并不像预期的那样起作用。
Dao函数
@Transaction
@Query("SELECT * FROM AllocationPercentage WHERE id IN (:ids)")
fun getByIds(ids: List<Long>): LiveData<List<AllocationPercentageWithDetails>>
下面是我在ViewModel中观察它的方式:
class Allocatio