I'm trying to use the android ViewModel and LiveData, but I getting an error when I want to create the observe. The first argument give me an error: wrong first argument type, and it is expect Lifecycleowner. If I try to build it: incompatible types: FragStat cannot be converted to LifecycleOwner I'm using build tools 27.0.1 and the same for the support lib.
Here is my Fragment:
import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import com.example.konem.pubgstat.Adapters.StatAdapter;
import com.example.konem.pubgstat.LocalStatViewModel;
import com.example.konem.pubgstat.Models.LocalStat;
import com.example.konem.pubgstat.R;
import com.example.konem.pubgstat.Utils.LocalData.AppDatabase;
import com.example.konem.pubgstat.Utils.UserManager;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import java.util.List;
import static android.content.ContentValues.TAG;
@EFragment(R.layout.fragment_frag_stat)
public class FragStat extends Fragment {
private List<LocalStat> localStats;
private StatAdapter statAdapter;
private AppDatabase appDatabase;
private UserManager userManager;
@ViewById
RecyclerView rvStats;
@AfterViews
void init() {
appDatabase = AppDatabase.getAppDatabase(getActivity());
userManager = new UserManager(getActivity());
LocalStatViewModel mViewModel =
ViewModelProviders.of(this).get(LocalStatViewModel.class);
statAdapter = new StatAdapter(localStats);
rvStats.setLayoutManager(new LinearLayoutManager(getActivity()));
rvStats.setAdapter(statAdapter);
subscribetostats(mViewModel);
}
private void subscribetostats(LocalStatViewModel localStatViewModel) {
localStatViewModel.getList().observe(this, new Observer<List<LocalStat>>() {
@Override
public void onChanged(@Nullable List<LocalStat> localStats) {
}
});
}
}
Set the return type for getting records from DOA interface to
LiveData<List<LocatStat>> getAllStats();
Then get Live data in fragment,
LiveData<List<LocalStat> localStats;
Finally set Livedata observer in fragment
localStats.getList().observe(this, new Observer<List<LocalStat>>() {
@Override
public void onChanged(@Nullable List<LocalStat> localStats) {
// Update UI
}
});
So after few days trying I found the problem. You have to include this few lines in every fragment or activity, I thought is implemented by the appcombat but not...
private LifecycleRegistry mLifecycleRegistry;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLifecycleRegistry = new LifecycleRegistry(this);
mLifecycleRegistry.markState(Lifecycle.State.CREATED);
}
@Override
public void onStart() {
super.onStart();
mLifecycleRegistry.markState(Lifecycle.State.STARTED);
}
@NonNull
@Override
public Lifecycle getLifecycle() {
return mLifecycleRegistry;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.