首页 > 解决方案 > 从sqlite数据库获取数据作为livedata android

问题描述

我有应用程序,现在我想使用新的 android 功能更新它的某些部分。我的数据库是 SQLite,由于某种原因我不能使用 Room 数据库。所以我想在我的应用程序中使用视图模型和实时数据。问题是我无法从我的数据库中获取数据作为 Livedata。这是我从数据库获取数据的代码:

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;
}

如您所见,我将数据作为 itema 列表获取。现在我怎样才能将数据作为 Livedata<List> 获取。任何帮助将不胜感激。

标签: androidsqliteandroid-livedata

解决方案


我认为你可以做这样的事情。只需LiveData<<ArrayList<itema>>从函数返回并稍后在后台对其进行变异以反映结果。

例如。

    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;
}

以及您使用的地方只是观察者。

getDataContact2().observe(this, Observer<List<itema>>{ it->
  //some action on change of data
})

推荐阅读