Home > other >  LiveData with sqlite not updating UI
LiveData with sqlite not updating UI

Time:09-22

Here is the code for fetching data from sqlite database as livedata

private LiveData<ArrayList<Message>> loadAllMessagesOfThisChat() {
  final  MutableLiveData<ArrayList<Message>> messages= new MutableLiveData<>();

    ArrayList<Message> messagesArrayList = new ArrayList<>();

    SQLiteDatabase mDb = messagesDbHelper.getReadableDatabase();
    String[] projection = {
            MessageDetails.COLUMN_SENDER_UID,
            MessageDetails.COLUMN_MESSAGE,
            MessageDetails.TIMESTAMP_ID,
            MessageDetails.COLUMN_IMAGE_URI};
    Cursor cursor = mDb.query("receiver"   receiverUID, projection, null, null, null, null, null);
    int messageColumnIndex = cursor.getColumnIndex(MessageDetails.COLUMN_MESSAGE);
    int timestampColumnIndex = cursor.getColumnIndex(MessageDetails.TIMESTAMP_ID);
    int imageURIColumnIndex = cursor.getColumnIndex(MessageDetails.COLUMN_IMAGE_URI);
    int senderUIDColumnIndex = cursor.getColumnIndex(MessageDetails.COLUMN_SENDER_UID);
    while (cursor.moveToNext()) {
        Message message = new Message();
        message.setMessage(cursor.getString(messageColumnIndex));
        message.setImageUrl(cursor.getString(imageURIColumnIndex));
        message.setTimestamp(String.valueOf(cursor.getInt(timestampColumnIndex)));
        message.setUserID(cursor.getString(senderUIDColumnIndex));
        messagesArrayList.add(message);

    }
    messages.setValue(messagesArrayList);
    cursor.close();
    mDb.close();

    return (LiveData<ArrayList<Message>>) messages;
}

And I am using this in oncreate

LiveData<ArrayList<Message>> messages=loadAllMessagesOfThisChat();
    messages.observe(this, new Observer<ArrayList<Message>>() {
        @Override
        public void onChanged(ArrayList<Message> messages) {
            messagesAdapter.setMessages(messages);
        }
    });

But whenever i send a new message , it gets added in the database but change are not reflected in the UI until i restart my activity

Seems like observer is not doing anything

I am trying to use livedata with sqlite as i cannot use room for some reasons

CodePudding user response:

In your code. There is no asynchronous programming, so your code runs synchronously. It means you call setValue() first and then return the object of liveData. Which results in not doing anything because observer is setting after calling setValue() method. So observer did not observe anything.

I can think of 2 solutions

  1. Either you do asynchronous programming and fetch the data on background thread
  2. Or you can just return the value of ArrayList directly. In this case you do not have to use liveData.

CodePudding user response:

Don't use livedata in native sqllite instead, call notifyDataSetChanged() after updating your database. This will re-populate the listview with latest values.

Alternatively, you can call reCreate() to restart your activity.

  • Related