skip to Main Content

I have on app, now i want to update some parts of it using new android features. my database is SQLite
and for some reason i can not use Room database. so i want to use viewmodel and live data in my app.
problem is i cant how to get data from my db as Livedata.
this is my code to get data from db:

public ArrayList<itema> getDataContact2 () {

    SQLiteDatabase db = myhelper.getReadableDatabase();
    String[] columns = {DBhelper.CONID,DBhelper.CONNAME, DBhelper.PHONE1, DBhelper.PHONE3, DBhelper.ADDRESS, DBhelper.TELEGRAM,DBhelper.INSTAGRAM,DBhelper.PHONE2,DBhelper.IMAGE,DBhelper.INFO,DBhelper.WHATSUP,DBhelper.EMAIL,DBhelper.EVENT,DBhelper.EVENTTITLE};
    Cursor cursor = db.query(DBhelper.TABLE_CONTACT, columns, null, null, null, null, DBhelper.CONNAME);
    ArrayList<itema> myList=new ArrayList<>();


    while (cursor.moveToNext()) {
       try {
           int id = cursor.getInt(cursor.getColumnIndex(DBhelper.CONID));
           String name = cursor.getString(cursor.getColumnIndex(DBhelper.CONNAME));
           String phone1 = cursor.getString(cursor.getColumnIndex(DBhelper.PHONE1));
           String phone2 = cursor.getString(cursor.getColumnIndex(DBhelper.PHONE2));
           String phone3 = cursor.getString(cursor.getColumnIndex(DBhelper.PHONE3));
           String add = cursor.getString(cursor.getColumnIndex(DBhelper.ADDRESS));
           String date = cursor.getString(cursor.getColumnIndex(DBhelper.EVENT));
           String telegram = cursor.getString(cursor.getColumnIndex(DBhelper.TELEGRAM));
           String insta = cursor.getString(cursor.getColumnIndex(DBhelper.INSTAGRAM));
           String whatsup = cursor.getString(cursor.getColumnIndex(DBhelper.WHATSUP));
           String email = cursor.getString(cursor.getColumnIndex(DBhelper.EMAIL));
           String info = cursor.getString(cursor.getColumnIndex(DBhelper.INFO));
           String image = cursor.getString(cursor.getColumnIndex(DBhelper.IMAGE));
           String datetitle = cursor.getString(cursor.getColumnIndex(DBhelper.EVENTTITLE));
           itema item=new itema(id,name,phone1,phone2,phone3,add,email,insta,telegram,whatsup,image,info,date,datetitle);

           myList.add(item);
       }
       catch (Exception e){
           e.printStackTrace();
       }

    }
    cursor.close();
    return myList;
}

as u can see, i get data as a list of itema. now how can i get data as Livedata<List>.
any help will be appreciated.

2

Answers


  1. I think you can do something like this. Just return LiveData<<ArrayList<itema>> from the function and mutate it later in the background to reflect the result back.

    for ex.

        public LiveData<ArrayList<itema> getDataContact2 () {
    
        LiveData<ArrayList<itema>> resultList = MutableLiveData<List<itema>>();
    
        //use some background thread to get data from Sqlite here below......
        SQLiteDatabase db = myhelper.getReadableDatabase();
        String[] columns = {DBhelper.CONID,DBhelper.CONNAME, DBhelper.PHONE1, DBhelper.PHONE3, DBhelper.ADDRESS, DBhelper.TELEGRAM,DBhelper.INSTAGRAM,DBhelper.PHONE2,DBhelper.IMAGE,DBhelper.INFO,DBhelper.WHATSUP,DBhelper.EMAIL,DBhelper.EVENT,DBhelper.EVENTTITLE};
        Cursor cursor = db.query(DBhelper.TABLE_CONTACT, columns, null, null, null, null, DBhelper.CONNAME);
        ArrayList<itema> myList=new ArrayList<>();
    
    
        while (cursor.moveToNext()) {
           try {
               int id = cursor.getInt(cursor.getColumnIndex(DBhelper.CONID));
               String name = cursor.getString(cursor.getColumnIndex(DBhelper.CONNAME));
               String phone1 = cursor.getString(cursor.getColumnIndex(DBhelper.PHONE1));
               String phone2 = cursor.getString(cursor.getColumnIndex(DBhelper.PHONE2));
               String phone3 = cursor.getString(cursor.getColumnIndex(DBhelper.PHONE3));
               String add = cursor.getString(cursor.getColumnIndex(DBhelper.ADDRESS));
               String date = cursor.getString(cursor.getColumnIndex(DBhelper.EVENT));
               String telegram = cursor.getString(cursor.getColumnIndex(DBhelper.TELEGRAM));
               String insta = cursor.getString(cursor.getColumnIndex(DBhelper.INSTAGRAM));
               String whatsup = cursor.getString(cursor.getColumnIndex(DBhelper.WHATSUP));
               String email = cursor.getString(cursor.getColumnIndex(DBhelper.EMAIL));
               String info = cursor.getString(cursor.getColumnIndex(DBhelper.INFO));
               String image = cursor.getString(cursor.getColumnIndex(DBhelper.IMAGE));
               String datetitle = cursor.getString(cursor.getColumnIndex(DBhelper.EVENTTITLE));
               itema item=new itema(id,name,phone1,phone2,phone3,add,email,insta,telegram,whatsup,image,info,date,datetitle);
    
               myList.add(item);
           }
           catch (Exception e){
               e.printStackTrace();
           }
    
        }
        cursor.close();
        resultList.postValue(myList)
        //some background operation end here 
    
        return resultList;
    }
    

    and where you are using just observer it.

    getDataContact2().observe(this, Observer<List<itema>>{ it->
      //some action on change of data
    })
    
    Login or Signup to reply.
  2. Your should maintain a variable typed MutuableLiveData<ArrayList<itema>> in the viewmodel where you used it.

    And update that livedata by post value from your db function getDataContact2.

    liveData.postValue(dbData)

    Besides this you can create a livedata from the list in your db function getDataContact2 and return that if you really want to be.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search