Android db loading chat for chat application - android

I'm creating a chat application and I need a little bit of guidance. I am using a sqlite database to store the chat as it comes in. I want my Activity, when it's open, to load the chat history from that particular chat and to continue to refresh as new chat is entered (my main question is how do I go about doing this).
Should I use a CursorAdapter with an inital cursor of a query containing the chat for that conversation and set it as the adapter for the ListView? I have tried this, but the data doesn't refresh when I insert into the database.
I know I didn't provide any code, but general conversation about what the best way is to go about this is welcome. Thanks!
Let me also mention that I need this to work for android 2.3.3 (API 10) and up (CursorLoader and all that is not available until API 11 which I did read a little about). The other thing I can do is use an ArrayAdapter and add chats directly into that (if the activity is open) and also insert it into the db in case it is not and then on onResume(), clear the ArrayAdapter and query all the convo once and readd each. Would this be the most optimal way?

Have a look at LoaderManager. Really simple and does exactly what you want.
It is available for 2.3.3 using the compatibility library.
http://developer.android.com/reference/android/app/LoaderManager.html

Related

How to get values from my data base and feed them in a string array

I am designing a to-do app in android I've made a login system and according to that, I want that the users who create the todos go to my database and gets stored there.
I am willing to get everything on the cloud so as to make it accessible from anywhere. Kindly guide me for this project because I am new to databases.
(I have my recyclerView just need to know the method to get everything in a string array)
First you need to connect the Application to Android Built-in Database Consider the Given Example Code,SQLite Demo Application. You can go through it, it's simple example.Then You can Perform Queries over the Database.

How to create recycler view like WhatsApp chat

I have created a list view just like a whatsapp chats list which is working fine it is loading data from server and setting its view but the problem is when the device is offline it cannot show the view which was previsouly created so my question is how to save the list view offline so that when the device is online my app recieves views from server and if the device is offline then the app should show views which were previously loaded.
And if possible please share some tutorials related to this.
You need to save your data locally using database. The most common option is sqlite. You can try this tutorial to understand what to do: http://www.tutorialspoint.com/android/android_sqlite_database.htm there are some options like Sugar Orm (or any other ORM) which uses sqlite in the background but makes it easy to work with objects (the shortage is that you are very limited when it comes to relations between tables and such). Anyway, this is their website: http://satyan.github.io/sugar/
You can try using Firebase database for your chats. You Just need one line of code to enable chats offline persistence i.e
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
For more take a look on offline-capabilities.

saving chats using chatadapter

I am using a custom chatadapter that I use to make a simple chat program with multiple user. However as soon as I leave the chat activity all the messages are gone. What would I have to do to save all the messages of that chat history and then put them back in the chat window when the user opens up the chat? The chats are in a listview. I am new to JAVA so I am not fully aware on how to go about this. If I understand correctly I would have to use SQL. Can anyone guide me as to the efficient way to do this? Thanks
Best thing to go about this is use SQLlite database. As chats volumes may be high and better to avoid service call to load huge data set. Also, local database required for handling network connect/disconnect(you might need to retry messages which are not being sent during network glitch).
You can take reference from Applozic's open source client repo.
If you intend to keep more than few messages and chats in your app, and access them efficiently, then yes - database would definitely be the good way to approach this problem.

How could i manually update the recyclerview of my app?

I am new to Android development. I intended on users to submit their information via Google Docs until I created a website for my app. I currently have a recyclerview to display posts. These posts consist of imageviews and textviews. My question is what is the most efficient way I could update my recylerview to display their submitted information if approved. If not plausible what would you suggest to manage the content in my program? Also keep in my mind I plan on developing the same application in Ios in the future and would like for each app to display the same information.
You could represent the data fetched from your web service in an SQLite Database (a good tutorial on how to create/access sqlite databases in android can be found here:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/).
Then you could use a CursorLoader and a CursorAdapter for the RecyclerView and swap the cursor when the loader finishes.
If you are modifying your dataset simply call onContentChanged() and your loader will requery and publish the results automatically.
Here you'll find a decent CursorAdapter implementation for RecyclerView: https://gist.github.com/skyfishjy/443b7448f59be978bc59
And here is a good documentation on how to use the CursorLoader with the LoaderCallbacks: https://developer.android.com/training/load-data-background/setup-loader.html

Android SQLite database lifecycle

I'm new to android. I want to ask about SQLite database lifecycle. Basicaly, I want to include the insert data method and viewing data method in the main class (which basicaly called 1st when we open the app). Then I'm going to make some update to the database in another activity. What I want to ask is, when I'm going to open the app for the next time, which data that will be showed? Is it the 1st data that I inserted with insert method OR the updated one?
I don't understand exactly your question. So I hope this will answer it. There are 4 operations, you should implement: create, read, update, delete. For example when you have a contact table with id, name, phone number then you can create (John, 012345). If you then call the read method with id 1, you will get (John, 012345). But if you update this before you read it, you obviously get the updated item - the database didn't delete anything when you close the activity. I highly recommend you to create a new class to handle those crud-operations, because you will get crazy, when you wanna change the update operation and must search it in every single line from your app. I also recommend you to read this blog. It saved me a lot of time:
http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/
As far as I understand your question,
my answer is -> You should see the updated data, because Db is same throughout the app.
The data that will be shown is the updated one , because SQL operations are immediate. It is very unlikely that onUpdate() will not update the database until and unless it encounters any error. you can see that itself using a CursorLoader attached with your cursor after running a query command. This can be done in an activity just displaying the contents of the database.
For database operations, It is beneficial to use Content Providers cause then you can extend your apps to widgets, share data with other apps etc.

Categories

Resources