【问题标题】:Dynamic parameters in viewmodel Androidviewmodel Android中的动态参数
【发布时间】:2019-11-15 01:13:22
【问题描述】:

如何在 Android 中重用 ViewModel 的参数?我有一个循环查看器,其中填充了基于参数的数据库中的数据:selectedDate。如何在片段中重用它,以这种方式刷新回收查看器?到目前为止,我使用的是ViewModelFactory:

public class MyViewModelFactory implements ViewModelProvider.Factory {

    private Application mApplication;
    private String mParam;


    public ShiftViewModelFactory(Application mApplication, String param) {
        mApplication=mApplication;
        mParam = param;
    }


    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        return (T) new MyViewModel(mParam);
    }
}

我正在将其加载到onCreateView:

myViewModel = ViewModelProviders.of(this, new myViewModelFactory(selectedDate)).get(MyViewModel.class);

如何更改参数selectedDate然后刷新数据?

这是我的视图模型

public class ShiftsViewModel extends ViewModel implements IShiftMapCallbackListener {

private MutableLiveData<List<ShiftModel>> coworkerList;
private MutableLiveData<String> messageError;
private IShiftMapCallbackListener coworkerInfoCallbackListener;


public ShiftsViewModel() {

    coworkerInfoCallbackListener = this;
}

private void loadCoworkerList() {
    final List<ShiftModel> tempList = new ArrayList<>();
    DatabaseReference coworkerRef = FirebaseDatabase.getInstance()
            .getReference(Common.MAP)
            .child(Common.DATA)
            .child(selectedDate);

    coworkerRef.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot itemSnapshot : dataSnapshot.getChildren())
                if (!itemSnapshot.child("Operator").getValue().equals("")) {
                    ShiftModel model = itemSnapshot.getValue(ShiftModel.class);
                    tempList.add(model);
                }
            coworkerInfoCallbackListener.onCoworkerInfoLoadSuccess(tempList);
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            coworkerInfoCallbackListener.onCoworkerInfoLoadFailed(databaseError.getMessage());
        }
    });
}

public MutableLiveData<List<ShiftModel>> getCoworkerList() {
    if (coworkerList == null){
        coworkerList = new MutableLiveData<>();
        messageError = new MutableLiveData<>();

        loadCoworkerList();
    }
    return coworkerList;
}

public MutableLiveData<String> getMessageError() {
    return messageError;
}

@Override
public void onCoworkerInfoLoadSuccess(List<ShiftModel> shiftModels) {
    coworkerList.setValue(shiftModels);
}

@Override
public void onCoworkerInfoLoadFailed(String message) {
    messageError.setValue(message);
}
}

我要改的​​参数是selectedDate:

DatabaseReference coworkerRef = FirebaseDatabase.getInstance()
            .getReference(Common.MAP)
            .child(Common.DATA)
            .child(selectedDate);

然后在片段中我这样称呼它:

shiftsViewModel.getCoworkerList().observe(this, shiftModel -> {

        shiftMapAdapter = new ShiftMapAdapter(ShiftsFragment.this.getContext(), shiftModel, operatorShow);

        coworker_recycler.setAdapter(shiftMapAdapter);
        coworker_recycler.setLayoutAnimation(layoutAnimationController);
    });

那么我怎样才能将日期从“2019-11-14”更改为“2019-11-15”

【问题讨论】:

  • 为什么将值传递给工厂而不是直接传递给视图模型?然后你可以有一个updateSelectedDate 方法来更新日期并再次触发查询
  • 我编辑了我的问题,添加了视图模型,你能看一下吗?

标签: android viewmodel mutablelivedata viewmodelproviders


【解决方案1】:

您可能希望从使用工厂参数转变为使用本质上是视图模型数据过滤器。您必须:

  1. 定义你的“过滤器”:private final MutableLiveData&lt;String&gt; filter = new MutableLiveData&lt;&gt;();
  2. 创建模型后,使用某个值初始化您的“过滤器”(可能使用您当前已经传递到工厂的值:filterText.setValue(...)
  3. 通过转换定义视图模型的实时数据(输入是过滤器):
Transformations.switchMap(
    filter, (Function<String, LiveData<...>) input -> {
...
}
  1. 定义一些更改过滤器的方法 - 每一次这样的更改都会让您获得新的数据观察:
void setFilter(String filter) {
    this.filter.setValue(filter);
}

【讨论】:

    【解决方案2】:

    您可以在没有 ViewModelFactory 的情况下执行此操作。只需创建函数来设置和更新 ViewModel 中的 selectedDate,然后在您的视图中调用该函数

    class FooViewModel: ViewModel() {
    
        var selectedDate: String = ""
    
        fun setDate(date: String) {
            selectedDate = date
        }
    }
    

    在您的 ViewModel 中传递给 variabel 的任何值都将共享给应用相同 ViewModel 的任何视图,您的不良活动和片段可以获得相同的值

    【讨论】:

    • 你能看一下吗,我添加了viewmodel。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-26
    • 2011-12-01
    相关资源
    最近更新 更多