Where / How to Implement SQLiteDatabase and AsyncTask in TabActivity
I have basically finished an application I am developing (my very first!), Except for implementing any data persistence mechanism. I was researching options a while ago and decided that SQLiteDatabase was the best choice, but it was difficult to dive into exactly how to implement it, so I left it until I finished the rest of the program. Since then I have just tested it by re-entering all values โโevery time I run it on my device.
The application is launched mainly from the screen. The user adds players (via menu and dialogue). Once the list of players is complete, the user clicks "Save" in the dialog box. Then the row table for each player is dynamically inflated and includes a Button, multiple ImageViews and a TextView, all displaying different attributes of each Player. It doesn't really matter to my question.
So, my application will basically keep track of Player objects. I have a database object, all built using this as a guide: http://www.androidhive.info/2011/11/android-sqlite-database-tutorial/ , swapping "contacts" for "players" for the most part ...
What is difficult for me to understand is exactly where to implement the "save" and "load" of each player object, as well as what to do in the main onCreate, onResume and onStop operations. I've been researching this for hours and I found out that I have to implement AsyncTask as well, but it's even more complicated and I can't even get the basic data loading into my brain, let alone do it asynchronously.
Here's a good example of how to implement both AsyncTask and SQLiteDatabase in a book I read titled "Android for Programmers: An App-Driven Approach" in the Deitel Developer Series, but in their example they use CursorAdapter in ListActivity, so I don't know how to do it for my situation. (Mine is the TabActivity, as I explained above, it's just the View Players tab.)
It makes sense for me to add players to the database when the user clicks save in the Add Players dialog, but I don't understand where to reload the Player objects, or where to reset each Player table row.
I wish I was posting the code to be more precise, but this is more of a general question and I would rather not publish my entire application as it was about a thousand lines long. Any help would be greatly appreciated, I try not to post every little thing here, but this stuff has disappointed me for too long!
source to share
I have the same book as you. I followed the AddressBook app (which works with DatabaseConnector) and it helped me quite a bit. Now I can share my code with you (I followed another Android tutorial for login / registration via SQL / PHP scripts) and then successfully moved it to use ASyncTask. However, the part I'm currently working on is overriding the local SQLite db: I need to get it to keep the user's logged in email, etc. so that I can display this information on the screen. Also, I have a page where a user can provide additional information (age, nationality, etc.) and the application will then save that data to a SQL server with the rest of their account information.
So, as I said, I would be happy to share this code with you, but this is not exactly what you are looking for, this is its network database, not a local SQLite db. It will have a SQLite db aspect, I just haven't finished this part.
Finally, I suggest AddressBook from your Deitel book, it really helped me.
source to share
AsyncTasks is a pain when it comes to configuration changes (like rotating your phone) and it's easy to create memory leaks. You have to use Loader. Ignore the name, it can also be used to write data - basically for every operation done in the background. More specifically, I would use AsyncTaskLoader and in the method loadInBackground()
I would put a SELECT or INSERT database helper with data. The database helper has to be an extension of SQLiteOpenHelper and it can only be created once, so I suggest putting it in the application context and grabbing the link from there.
Hope it helps.
source to share