Tag: viewmodel

Android架构组件LiveData

我正在尝试使用架构组件来实现一个简单的应用程序。 我可以使用Retrofit2从RestApi服务获取信息。 我可以在各自的Recyclerview中显示信息,当我旋转手机时,一切都按照原样进行。 现在我想过滤一个新types的对象(通过字符串) 有人可以用ViewModel指导我一点,我不知道最好的做法是什么…我正在使用MVVM … 这是我的ViewModel: public class ListItemViewModel extends ViewModel { private MediatorLiveData 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 […]

一个LiveData中有多个对象

我有这个实体: @Entity public class CartItem { @PrimaryKey(autoGenerate = true) private long id; private long itemId; private long serviceId; private String observation; @Ignore private Service service; @Ignore private Item item; } 正如你所看到的,它由其他对象组成,在这种情况下, Service和Item 。 在片段中,我以这种方式实现LiveData: CartItemViewModel model = CartItemViewModel.create(getActivity()); model.getLiveData().observe(this, new Observer<List>() { @Override public void onChanged(@Nullable List cartItems) { } 它正在工作, onChange方法正在按预期调用。 这是我在kotlin中的ViewModel: class CartItemViewModel […]

Android架构组件 – ViewModel Observable&Proguard

我无法使ViewModel组件与Proguard一起使用。 我已经不得不添加以下内容来防止由于NoSuchMethodException导致崩溃:init() 保持类com …. SlideshowViewModel {*;} 但是,我在活动中的观察员没有收到任何数据。 这工作正常,直到我启用Proguard,所以我知道Proguard是原因,我只是不知道为什么(新手Proguard在这里)。 我需要添加什么规则才能使观测值工作? 我有以下在我的ViewModel(Kotlin) val currentItem = MediatorLiveData<MediaItem>() ….后来… Timber.d("Setting next image: " + position + " out of " + mediaItemList.size) currentItem.value = mediaItemList[position] 和活动(Java) viewModel.getCurrentItem().observe(this, new Observer<MediaItem>() { @Override public void onChanged(@Nullable final MediaItem mediaItem) { Timber.d("Activity received new item"); } }); 在日志中,我收到:D / SlideshowViewModel:设置下一个图像:0出18 但是onChanged Observable中没有任何东西被解雇。

Kotlin匕首2 Android ViewModel注入错误

我试图在我的Android应用程序上使用匕首2从arch android库注入新的ViewModel。 从我看到的这个示例https://github.com/googlesamples/android-architecture-components/tree/e33782ba54ebe87f7e21e03542230695bc893818/GithubBrowserSample我需要使用这个: @MustBeDocumented @Target(AnnotationTarget.FUNCTION, AnnotationTarget.CONSTRUCTOR, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) @Retention(AnnotationRetention.RUNTIME) @MapKey internal annotation class ViewModelKey(val value: KClass<out ViewModel>) @Module abstract class ViewModelModule { @Binds @IntoMap @ViewModelKey(LoginViewModel::class) internal abstract fun bindLoginViewModel(viewModel: LoginViewModel): LoginViewModel @Binds @IntoMap @ViewModelKey(MainMenuViewModel::class) internal abstract fun bindSearchViewModel(viewModel: MainMenuViewModel): MainMenuViewModel @Binds internal abstract fun bindViewModelFactory(factory: ViewModelFactory): ViewModelProvider.Factory } @ApplicationScope @Component(modules = arrayOf(ApplicationModule::class, NetworkModule::class, ViewModelModule::class)) interface […]

Android Kotlin匕首2 ViewModel注入错误

我想在我的项目上使用新的ViewModel从拱门Android库。 我在github上学习了很多示例项目。 最后,我想在我的项目上实现这个架构,但是我无法构建它。 Gradle控制台: ….AppComponent.java:6: error: [dagger.android.AndroidInjector.inject(T)] kibar.app.ui.fragment.HomeFragmentViewModel cannot be provided without an @Inject constructor or from an @Provides-annotated method. e: public abstract interface AppComponent { e: ^ e: kibar.app.ui.fragment.vpresenter.home.HomeFragmentViewModel is injected at e: kibar.core.di.ViewModelModule.bindHomeViewModel(model) e: java.util.Map<java.lang.Class<? extends android.arch.lifecycle.ViewModel>,javax.inject.Provider<android.arch.lifecycle.ViewModel>> is injected at e: kibar.core.di.viewmodel.ViewModelFactory.<init>(creators) e: kibar.core.di.viewmodel.ViewModelFactory is injected at e: kibar.app.ui.fragment.HomeFragment.viewModelFactory e: kibar.app.ui.fragment.HomeFragment is injected […]

Android中的ViewModelProviderFactory在kotlin中

我正在试验来自Google的架构组件 。 具体来说,我想实现一个ViewModelProvider.Factory来创建一个ViewModel,它需要构造函数参数,如下所示: class MyFactory(val handler: Handler) : ViewModelProvider.Factory { override fun <T : ViewModel?> create(modelClass: Class<T>?): T { return MyViewModel(handler) as T } } 我的ViewModel看起来像这样: class MyViewModel(val handler: Handler) : ViewModel() 任何人都知道如何避免讨厌的演员: return MyViewModel(handler) as T