英文:
Updating data Android MVVM
问题
我正在使用MVVM架构开发一个应用程序,但我对于在需要从事件(例如单击按钮)更新或插入数据时的最佳方法感到有些困惑。
我在订阅观察者以跟踪更改方面有以下代码,当我在添加产品到订单后打开片段时,这段代码运行良好:
shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer<Resource<OrderWithProducts>>() {
@Override
public void onChanged(Resource<OrderWithProducts> pedidoResource) {
if (pedidoResource != null) {
Log.d(TAG, "onChanged: status: " + pedidoResource.status);
switch (pedidoResource.status) {
case LOADING: {
adapterPedido.displayLoading();
break;
}
case ERROR: {
adapterPedido.hideLoading();
if(pedidoResource.data != null) adapterPedido.setList(pedidoResource.data);
break;
}
case SUCCESS: {
if (pedidoResource.data != null) {
adapterPedido.hideLoading();
adapterPedido.setList(pedidoResource.data);
break;
}
}
}
}
});
至于更新订单,我会这样处理:
public void updateOrder() {
pedidoRepository.setOrderConfirm(OrderWithProducts.getValue().data.getPedido().getId());
pedidoConProductos.getValue().data.getPedido().setConfirmado(true);
}
但我认为应该有更好的方法,因为片段不会自动更新来自片段的数据。
在更新后,适配器应该也会变化。
英文:
I'm working in an App using the architecture MVVM, and im a little confused about how its the best way when you need to update or insert data from an event like when you click a button.
I have this in the suscribe Observers to keep track on changes, and this work well when i open the fragment after add Products to and order:
shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer<Resource<OrderWithProducts>>() {
@Override
public void onChanged(Resource<OrderWithProducts> pedidoResource) {
if (pedidoResource != null) {
Log.d(TAG, "onChanged: status: " + pedidoResource.status);
switch (pedidoResource.status) {
case LOADING: {
adapterPedido.displayLoading();
break;
}
case ERROR: {
adapterPedido.hideLoading();
if(pedidoResource.data != null) adapterPedido.setList(pedidoResource.data);
break;
}
case SUCCESS: {
if (pedidoResource.data != null) {
adapterPedido.hideLoading();
adapterPedido.setList(pedidoResource.data);
break;
}
}
}
}
});
}
And to update the order i do this:
public void updateOrder() {
pedidoRepository.setOrderConfirm(OrderWithProducts.getValue().data.getPedido().getId());
pedidoConProductos.getValue().data.getPedido().setConfirmado(true);
}
But i think it has to be a better way, because the Fragment doesnt automatically update the data from the Fragment.
After updating this the Adapter should change.
答案1
得分: 1
尝试
shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer<List<OrderWithProducts>>() {
@Override
public void onChanged(List<OrderWithProducts> data) {
adapterPedido.setList(data);
}
});
shopViewModel.isLoading().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
@Override
public void onChanged(Boolean isLoading) {
if(isLoading) {
adapterPedido.displayLoading();
} else {
adapterPedido.hideLoading();
}
}
});
并且
public void updateOrder() {
pedidoRepository.setOrderConfirm(OrderWithProducts.getValue().data.getPedido().getId());
// pedidoConProductos.getValue().data.getPedido().setConfirmado(true);
}
如果来自 getOrderWithProducts()
的原始 LiveData 是从 Room DAO 作为 LiveData<List<T>>
提供的,那么它将自动进行更新。
英文:
Try
shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer<List<OrderWithProducts>>() {
@Override
public void onChanged(List<OrderWithProducts> data) {
adapterPedido.setList(data);
}
});
shopViewModel.isLoading().observe(getViewLifecycleOwner(), new Observer<Boolean>() {
@Override
public void onChanged(Boolean isLoading) {
if(isLoading) {
adapterPedido.displayLoading();
} else {
adapterPedido.hideLoading();
}
}
});
And
public void updateOrder() {
pedidoRepository.setOrderConfirm(OrderWithProducts.getValue().data.getPedido().getId());
// pedidoConProductos.getValue().data.getPedido().setConfirmado(true);
}
If the original LiveData provided from getOrderWithProducts()
comes from a Room DAO as LiveData<List<T>>
, then it will auto-update accordingly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论