Try to search your question here, if you can't find : Ask Any Question Now ?

get data from sqlite database as livedata android

HomeCategory: stackoverflowget data from sqlite database as livedata android
Avatarsourav asked 6 days ago

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>.
any help will be appreciated.

1 Answers
Best Answer
AvatarFernando answered 6 days ago
Your Answer

8 + 10 =

Popular Tags

WP Facebook Auto Publish Powered By : XYZScripts.com