2

I'm using room with livedata. when I select from viewmodel, I can't see anything. But when I select directly to dao, I can see items. What's the problem?

Room has no mutable livedata so I choosed livadata... but it doesn't work. please help me. I dont understand my problem.

when I entered "http://localhost:8080 " using debugImplementation 'com.amitshekhar.android:debug-db:1.0.6' and searched the result query, the result is right. I think the problem is using livedata. I logged in viewmodel, and that is null.

@Dao
public interface MemoDao {
     @Query("select * from memolist where date between :fromDate and :toDate and isDeleted='true' order by date")
    LiveData<List<MemoEntity>> selectAll(String fromDate, String toDate);


    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertMemo(MemoEntity memo);

    @Update
    void updateMemo(MemoEntity memo);

    @Delete
    void deleteMemo(MemoEntity... memo);

}

@Database(entities = {MemoEntity.class}, version = 2)
public abstract class AppDatabase extends RoomDatabase {
    private static AppDatabase appDatabase;

    public abstract MemoDao MemoDao();

    public static AppDatabase getInstance(Context context){
        appDatabase = Room.databaseBuilder(context.getApplicationContext(), AppDatabase.class, "memolist")
                .addMigrations(MIGRATION_1_2)
                .build();

        return appDatabase;
    }
    static final Migration MIGRATION_1_2= new Migration(1, 2) {
        @Override
        public void migrate(SupportSQLiteDatabase database) {
            database.execSQL("ALTER TABLE memolist ADD COLUMN id INTEGER NOT NULL DEFAULT 0");
        }
    };
}

public class MemoViewModel extends ViewModel {

    LiveData<List<MemoEntity>> memoModel;
    AppDatabase mRepository;

    public void init(Context context,String fromDate, String toDate){
        mRepository =  AppDatabase.getInstance(context);
        memoModel = mRepository.MemoDao().selectAll(fromDate,toDate);

    }

    public LiveData<List<MemoEntity>> memoList(){
        Log.d("TAG",memoModel.getValue().get(0).getId()+"");
        return memoModel;
    }
    public LiveData<List<MemoEntity>> memoListByTag(String tag, String fromDate, String toDate){
        memoModel = mRepository.MemoDao().selectAllByTag(tag,fromDate,toDate);
        return memoModel;
    }

}
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        fromDate = (String)dataHolder.popDataHolder("fromDate");
        toDate = (String)dataHolder.popDataHolder("toDate");
         memoViewModel = ViewModelProviders.of(this).get(MemoViewModel.class);
         memoViewModel.init(this.getContext(),fromDate,toDate);
         memoViewModel.memoList().observe(this, new Observer<List<MemoEntity>>(){
             @Override
             public void onChanged(List<MemoEntity> memoEntities) {
                listItems = memoViewModel.memoList().getValue();
                recyclerViewAdapter.notifyDataSetChanged();
             }
         });

    }
Prashant Sable
  • 1,003
  • 7
  • 19
user9628944
  • 131
  • 7

1 Answers1

0

change your MemoViewModel class like this

public class MemoViewModel extends ViewModel {

MutableLiveData<List<MemoEntity>> memoModel=new MutableLiveData();
AppDatabase mRepository;

public void init(Context context,String fromDate, String toDate){
    mRepository =  AppDatabase.getInstance(context);
    memoModel.postValue(mRepository.MemoDao().selectAll(fromDate,toDate))
}

public MutableLiveData<List<MemoEntity>> memoList(){
     return memoModel;
}
public LiveData<List<MemoEntity>> memoListByTag(String tag, String fromDate, String toDate){
    memoModel = mRepository.MemoDao().selectAllByTag(tag,fromDate,toDate);
    return memoModel;
}

}

change your dao class like

 @Dao
public interface MemoDao {
     @Query("select * from memolist where date between :fromDate and :toDate and isDeleted='true' order by date")
    List<MemoEntity> selectAll(String fromDate, String toDate);


    @Insert(onConflict = OnConflictStrategy.REPLACE)
    void insertMemo(MemoEntity memo);

    @Update
    void updateMemo(MemoEntity memo);

    @Delete
    void deleteMemo(MemoEntity... memo);

}

MutableLiveData is a subclass of LiveData which is used for some of its properties (setValue/postValue) and using these properties we can easily notify the UI when onChange() is called.

Arunachalam k
  • 734
  • 1
  • 7
  • 20
  • it is not working. I have an error when using mutable livedata. that is "Not sure how to convert a Cursor to this method's return type" – user9628944 Dec 19 '19 at 10:42
  • see upvote if the above code works returns some value. I can't get it what u r trying to do please add some more code explain even clear – Arunachalam k Dec 20 '19 at 03:58
  • Did u ever try using room with mutablelivedata? room doesn't support mutablelivedata. so i tried just livedata, but it doesn't support memolist().observe – user9628944 Dec 20 '19 at 04:09
  • @user9628944 Please check latest one i have updated my code – Arunachalam k Dec 20 '19 at 06:40