考虑以下公开MutableLiveData的方法
方法A
class ThisViewModel : ViewModel() {
private val _someData = MutableLiveData(true)
val someData: LiveData<Boolean>
get() = _someData
}
// Decompiled Kotlin bytecode
public final class ThisViewModelDecompiled extends ViewModel {
private final M
在android蓝图示例的视图模型中,Google使用了以下模式:
private val _dialog = MutableLiveData<Dialog>()
val dialog : LiveData<FindaDialogFragment> = _dialog
我搜索谷歌是因为这个原因。总之,我们限制访问,使片段和活动不能修改viewModel中的数据。
我明白,但不能表示同情。真的只有一个原因吗?如果是这样的话,代码在viewModel中将更长,以限制对变量的访问。
private val name = MutableLiveData<String&g
当我想清除ListAdapter中的所有数据时,出现了以下错误
java.lang.UnsupportedOperationException
at java.util.Collections$UnmodifiableCollection.clear(Collections.java:1116)
at com.zhixin.wedeep.homepage.ui.AudioPlayerActivity$onComment$$inlined$apply$lambda$3$1$1.onChanged(AudioPlayerActivity.kt:300)
我已经向本地房间数据库发送了一个用户数据对象的实例,以缓存loginFragment.kt上Firebase登录流中包含的一些数据
val firebaseUser = FirebaseAuth.getInstance().currentUser
firebaseUser?.let {
// here we get access to some of the data provided by firebase auth and pass it to a new user table in the local db
//this s
我在LiveData中使用Android架构。我有个像这样的东西
public class User {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastNa
目前,我开始使用数据绑定库,在我的视图模型中,我有一个扩展的LiveData对象:
public class ScannerViewModel extends AndroidViewModel {
/** MutableLiveData containing the scanner state to notify MainActivity. */
public ScannerLiveData scannerLiveData;
ScannerLiveData:
public class ScannerLiveData extends MutableLiveData<Sca
示例ViewModel:
public class NameViewModel extends ViewModel {
// Create a LiveData with a String
private MutableLiveData<String> mCurrentName;
public MutableLiveData<String> getCurrentName() {
if (mCurrentName == null) {
mCurrentName = new MutableLiveData&l
在我的Android项目中,我有一个类,其中一个函数运行一个协同器,我想使用一个测试(而不是单元测试)来测试这个类。
如下所示:
class DemoClass
{
fun demo(liveData: MutableLiveData<String>)
{
CoroutineScope(IO).launch {
val result = doStuff()
withContext(Main) { liveData.value = result }
}
}
}
在我的仪器测试类中,
在使用架构组件的Android应用程序中,我有以下视图模型:
public class MainViewModel extends AndroidViewModel {
private final MutableLiveData<List<String>> mUnchecked = new MutableLiveData<>();
private LiveData<List<String>> mChecked;
public void setUnchecked(List<String> list)
今天我有一些关于android上的mvvm和数据绑定的问题,
我正在尝试绑定视图上的对象属性。
我有一个对象(Model),它有一些属性,例如:
public String name;
public String title;
public int value;
我有一个带有livedata的ViewModel,像这样:
MutableLiveData<Object> _obj = new MutableLiveData<>();
public LiveData<Object> obj = _obj;
最后,我有一个这样的观点:
<layout>
我有下面的ViewModel
class MainViewModel @Inject constructor(private val mainRepository: MainRepository) : ViewModel(){
private var _user: LoggedInUser? = null
val allProjects: MutableLiveData<Array<Project>> = MutableLiveData()
fun setUser(loggedInUser: LoggedInUser){
最近,我被以下代码困住了。
public class NoteViewModel extends ViewModel {
private final MutableLiveData<List<Note>> notesLiveData = new MutableLiveData<>();
public NoteViewModel() {
LiveData<List<Note>> notesLiveDataFromRepository = NoteRepository.INSTANCE.getNotes
我想使用MutableLiveData来观察来自ViewModel的一些数据。问题是,我使用的是子类和父类,并且与LiveData有一些不兼容性。我想在科特林做的一个例子:
import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel
class Test : ViewModel() {
abstract class Parent(protected var id: Int)
class ChildFirst(id: Int) : Parent(id)
我知道Android MVVM,LiveData和DataBinding。但是,我有一个场景,其中我有许多输入UI字段,如电子邮件,密码,确认密码等。我可以映射这些字段与ViewModel。
public class LoginViewModel extends ViewModel {
public MutableLiveData<String> email = new MutableLiveData<>();
public MutableLiveData<String> password = new MutableLiveD
我有这个视图模型:
public class AmountViewModel extends ViewModel {
private MutableLiveData<String> amount = new MutableLiveData<String>();
public void setAmount(long value) {
amount.postValue(Long.toString(value));
}
public LiveData<String> getAmount() {
r
在将数据从json解析为MutableLiveData时,我可以转换数据吗?如果可能的话,该如何转换呢?
@TypeConverter
fun stringToDatas(string: String) :MutableLiveData<HashMap<String,Any>> {
val liveData = object : TypeToken<MutableLiveData<HashMap<String, Any>>>() {
//here i guess should return it
在下面的示例中,我希望公开如下所示的Int列表:
val test: LiveData<List<Int>>
get() = _test as LiveData<List<Int>>
private var _test = MutableLiveData(mutableListOf<Int>())
或者是另一种口味:
private var _test2 = MutableLiveData(mutableListOf<Int>())
val test2 = _test2 a
我通过包从服务器请求数据,并将其存储在获取下一批数据的array.To跟踪中-- class.In --通知diffObservers的addItems方法,并传递新项的列表:
class PackItems:MutableLiveData<ArrayList<GetPacksResponse.PackData>>() {
private var diffObservers=ArrayList<Observer<List<GetPacksResponse.PackData>>>()
private va
在ViewModel中,我有MutableLiveData<MutableList<object>>,我想要创建MutableLiveData<MutableList<object>>,但是我不知道如何从MutableList创建List。
以下是我要做的事:
class AppViewModel : ViewModel() {
private val _tasks = MutableLiveData<MutableList<Task>>(mutableListOf())
val tasks: LiveD
目前,我使用一个服务通过retrofit进行网络调用,但我想将此模型更新为在LiveData中使用ViewModel。我正在努力解决的问题是如何设置Repository来更新livedata对象。 在我看到的示例中,人们在存储库中返回一个LiveData包装对象,如下所示 public LiveData<NewsResponse> getData(){
final MutableLiveData<DataResponse> data = new MutableLiveData<>();
apiService.getData().enqueue(n
我有这样的改造服务
public interface BrandsService {
@GET("listBrand")
Call<List<Brand>> getBrands();
}
然后,我有一个存储库从这样的api中获取数据
public class BrandsRepository {
public static final String TAG = "BrandsRepository";
MutableLiveData<List<Brand>> mutableLiveDa