0

I have a chatting application that saves the messages in a Sqlite database. I need to load all messages for each chat when clicked by user and show them in ListView in an activity. So i was wondering how to do this ? I thought of a couple of ways to do that

First option: get the data from database (messages i.e. Strings) before opening the messaging screen for the user in arrays and pass them through the intent when starting the messaging activity and then show the messages on onCreate() method.

Second Option: get the data (messages i.e. Strings) from database when activity is being created and show them.

Third option: Saving Persistent State according to Sqlite

Any other ideas are appreciated. Thanks.

1
  • 1
    try to making it in background thread with progress bar in the activity that will display the data Commented Feb 10, 2014 at 0:23

3 Answers 3

1

use 2nd option.just send the unique chat id to the chat screen and load all the messages of that chat from database in oncreate() method and then bind all messages to the listview.

Sign up to request clarification or add additional context in comments.

4 Comments

I will give it a try. But what if loading the messages at onCreate() takes time ? maybe a progress bar as suggested by @mohammed momn would be helpful here.
not progress bar show dialog box instead, and display some message and fetching messages from database couldnt take too much time, it will be in milli seconds.
Loaded in a perfect way without the need for threads. but i will look if a thread is needed later on when the data is larger in size.
you will need threads when records are in millions. but since you want to show chat messages , so you can fetch last 50 or 100 messages from database and when user reach to the top then again fetch other records and show to the user.
1

In Activity, loading sqlite db in others thread, and when it load finish, print it in your list view.

new Thread(new Runnable() {

    @Override
    public void run() {
        // TODO Auto-generated method stub

        //loading database
        mHandler.sendMessage(msg);
    }
}).start();

Handler mHandler = new Handler() {
    public void handleMessage(Message msg) {
        //update listview
    };
};

1 Comment

Sounds like the most reasonable way to load them in a thread at the onCreate(). I will give it a try and get back with the result. Thanks.
1

What I would do is to load them when you open the Activity, but I wouldn't load all messages but just the last X items. Most users won't scroll up to older messages 99% of the times.

You could for example load the last 10 messages and then (if the users make scroll up) load the other messages.

Luck!!

1 Comment

Sounds logical especially that it wont take time to select the last X messages, prepare them, and set up the listview

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.