更新数据 Android MVVM

huangapple go评论71阅读模式
英文:

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&lt;Resource&lt;OrderWithProducts&gt;&gt;() {
        @Override
        public void onChanged(Resource&lt;OrderWithProducts&gt; pedidoResource) {
            if (pedidoResource != null) {
                Log.d(TAG, &quot;onChanged: status: &quot; + 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&lt;List&lt;OrderWithProducts&gt;&gt;() {
    @Override
    public void onChanged(List&lt;OrderWithProducts&gt; data) {
        adapterPedido.setList(data);
    }
});

shopViewModel.isLoading().observe(getViewLifecycleOwner(), new Observer&lt;Boolean&gt;() {
    @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&lt;List&lt;T&gt;&gt; 提供的,那么它将自动进行更新。

英文:

Try

     shopViewModel.getOrderWithProducts().observe(getViewLifecycleOwner(), new Observer&lt;List&lt;OrderWithProducts&gt;&gt;() {
        @Override
        public void onChanged(List&lt;OrderWithProducts&gt; data) {
            adapterPedido.setList(data);
        }
    });

     shopViewModel.isLoading().observe(getViewLifecycleOwner(), new Observer&lt;Boolean&gt;() {
        @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&lt;List&lt;T&gt;&gt;, then it will auto-update accordingly.

huangapple
  • 本文由 发表于 2020年9月10日 03:49:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/63818637.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定