Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android架构组件LiveData_Android_Viewmodel_Android Architecture Components_Android Livedata - Fatal编程技术网

Android架构组件LiveData

Android架构组件LiveData,android,viewmodel,android-architecture-components,android-livedata,Android,Viewmodel,Android Architecture Components,Android Livedata,我正在尝试使用架构组件实现一个简单的应用程序。 我可以使用Refught2从RestApi services获取信息。 我可以在相应的Recyclerview中显示信息,当我旋转手机时,一切正常。 现在我想通过一种新的对象(通过字符串)进行过滤 有人能给我介绍一下ViewModel吗?我不知道最好的做法是什么。。。 我正在使用MVVM 这是我的ViewModel: public class ListItemViewModel extends ViewModel { private Med

我正在尝试使用架构组件实现一个简单的应用程序。 我可以使用Refught2从RestApi services获取信息。 我可以在相应的Recyclerview中显示信息,当我旋转手机时,一切正常。 现在我想通过一种新的对象(通过字符串)进行过滤

有人能给我介绍一下ViewModel吗?我不知道最好的做法是什么。。。 我正在使用MVVM

这是我的ViewModel:

public class ListItemViewModel extends ViewModel {
    private MediatorLiveData<ItemList> mList;
    private MeliRepository meliRepository;

    /* Empty Contructor.
     * To have a ViewModel class with non-empty constructor,
     * I have to create a Factory class which would create instance of you ViewModel and
     * that Factory class has to implement ViewModelProvider.Factory interface.
     */
    public ListItemViewModel(){
        meliRepository = new MeliRepository();
    }

    public LiveData<ItemList> getItemList(String query){
       if(mList == null){
           mList = new MediatorLiveData<>();
           LoadItems(query);
       }
    }

    private void LoadItems(String query){
        String queryToSearch = TextUtils.isEmpty(query) ? "IPOD" : query;

        mList.addSource(
                meliRepository.getItemsByQuery(queryToSearch),
                list -> mList.setValue(list)
        );
    }
}
公共类ListItemViewModel扩展了ViewModel{
私有数据列表;
私人储蓄;
/*空构造函数。
*要使ViewModel类具有非空构造函数,
*我必须创建一个Factory类,它将创建ViewModel和
*该工厂类必须实现ViewModelProvider.Factory接口。
*/
公共ListItemViewModel(){
meliRepository=新meliRepository();
}
公共LiveData getItemList(字符串查询){
if(mList==null){
mList=新的MediatorLiveData();
加载项(查询);
}
}
私有void LoadItems(字符串查询){
字符串queryToSearch=TextUtils.isEmpty(查询)?“IPOD”:查询;
mList.addSource(
meliRepository.getItemsByQuery(查询搜索),
列表->列表设置值(列表)
);
}
}
更新

我用生命周期库中的一个包解决了这个问题。。。

公共类ListItemViewModel扩展了ViewModel{
private final MutableLiveData mQuery=new MutableLiveData();
私人储蓄;
private LiveData mList=Transformations.switchMap(mQuery,text->{
返回meliRepository.getItemsByQuery(文本);
});
公共ListItemViewModel(MeliRepository存储库){
meliRepository=存储库;
}
公共LiveData getItemList(字符串查询){
返回mList;
}
}

@约翰:这是我的解决办法。我正在使用生命周期库,解决方案比我想象的要简单。谢谢

我更熟悉在Kotlin中执行此操作,但您应该能够足够轻松地将其转换为Java(或者现在是开始使用Kotlin的好时机:)……采用类似的模式,我相信您会这样做:

val query: MutableLiveData<String> = MutableLiveData()

val  mList = MediatorLiveData<List<ItemList>>().apply {
    this.addSource(query) {
        this.value = meliRepository.getItemsByQuery(query)
    }
}

fun setQuery(q: String) {
    query.value = q
}
val查询:MutableLiveData=MutableLiveData()
val mList=MediatorLiveData().apply{
this.addSource(查询){
this.value=meliRepository.getItemsByQuery(查询)
}
}
funsetquery(q:String){
query.value=q
}

我在下面的

Thx John中使用此模式。。。我试试这个@Guille只是好奇你能不能让它工作?我还没试过。。。我在空闲时间“学习”android。。。这个周末我会试试,我会告诉你的!:)
val query: MutableLiveData<String> = MutableLiveData()

val  mList = MediatorLiveData<List<ItemList>>().apply {
    this.addSource(query) {
        this.value = meliRepository.getItemsByQuery(query)
    }
}

fun setQuery(q: String) {
    query.value = q
}