我想在LiveData对象上的存储库中更新对象的成员变量。问题是,如果我调用了getValue()方法,尽管该值确实存在于我的RoomLibrary中,但我仍会收到NullPointerException。
我现在的问题是,如何从LiveData对象获取值,而无需调用observe()方法?(我无法在我的存储库中调用观察方法,因为该方法要我输入LifeCycleOwner-引用,该引用在我的存储库中不存在)。
有什么方法可以从LiveData对象中获取值?
我的架构如下所示:ViewModel->存储库-> Dao
你好!我希望这个视图可以滚动:
正如您所看到的,TAB1 中的“临时内容”只是用来模拟可能出现的临时内容。
我现在想做的是使整个页面可滚动。我设法做的是使 TAB1 的内容(即 TabLayout 的内容)可滚动,但这不是我想要做的。正如所描述的,整个页面应该是。
这是持有视图的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="at.codecrane.whosnext.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srcCompat="@android:color/darker_gray" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="100dp"
android:layout_height="100dp"
app:srcCompat="@drawable/ic_face"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView3"
android:layout_centerHorizontal="true"
android:text="Temp Name"
android:textSize="18sp" />
</RelativeLayout>
<android.support.v4.view.ViewPager
android:id="@+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.TabLayout
android:id="@+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="@color/colorPrimary">
<android.support.design.widget.TabItem
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="General" />
<android.support.design.widget.TabItem
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Statistics" />
</android.support.design.widget.TabLayout>
</android.support.v4.view.ViewPager>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
这是 ViewPager-Content 的片段布局:
<!-- TODO: Update blank fragment …Run Code Online (Sandbox Code Playgroud) android android-fragments android-viewpager android-tablayout
我已经按照谷歌直接编写的本教程.
我目前遇到的问题是班级userDao.save(response.body());内部的问题UserRepository.
private void refreshUser(final String userId) {
executor.execute(() -> {
// running in a background thread
// check if user was fetched recently
boolean userExists = userDao.hasUser(FRESH_TIMEOUT);
if (!userExists) {
// refresh the data
Response response = webservice.getUser(userId).execute();
// TODO check for error etc.
// Update the database.The LiveData will automatically refresh so
// we don't need to do anything else here besides updating the database
userDao.save(response.body());
}
});
}
Run Code Online (Sandbox Code Playgroud)
当我尝试在我的Android Studio版本中执行此操作时,我收到一条消息,指出此语言级别不支持Lambda表达式.
我知道的事实,我可以升级我的Android …