3

I'm basically finished an application I'm developing (my very first!), except for implementing any data saving mechanism. I looked into the options a while ago and decided the SQLiteDatabase is the best to go with, but it was difficult to wrap my head around how exactly to implement it, so I left it until I finished the rest of the program. Since then I've just been testing it by re-entering all the values each time I run it on my device.

The app starts with basically nothing on the screen. The user adds players (via the menu and a Dialog). Once the player list is finalized, the user clicks "save" in the Dialog. Then a Table Row for each player dynamically inflates and includes a Button, a few ImageViews, and a TextView, all displaying various attributes of each Player. That doesn't really matter for my question.

So, my application will basically just keep track of Player objects. I have the 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 I have trouble understanding is where exactly to implement the "saving" and "loading" of each player object, as well as what to do in the main Activity's onCreate, onResume, and onStop methods. I've been looking into this for many hours now, and I've learned I should also implement an AsyncTask, but that is even more complicated and I can't even get the basic data loading into my brain, let alone doing it asynchronously.

There's a good example of how to implement both AsyncTask and SQLiteDatabase in a book I'm reading called "Android for Programmers: An App-Driven Approach" in the Deitel Developer Series, but in their example they use a CursorAdapter in a ListActivity, so I don't know how to make it apply for my situation. (Mine is a TabActivity, what I explained above is just for a "View Players" Tab.)

It makes sense to 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 as well as where to reinflate each Player's table row.

I'm sorry I am not posting code to be more precise but it's more of a general question and I'd rather not post my entire app since it's about a thousand lines long. Any help would be greatly appreciated, I try not to post every little thing on here but this stuff has frustrated me too long!

S Fitz
  • 1,074
  • 15
  • 31

2 Answers2

1

I have the same book as you. I followed the AddressBook app (which deals with a DatabaseConnector) and it helped me quite a bit. Now, I can share my code with you (I have followed another Android Hive tutorial for login/register via SQL/PHP scripts) and then successfully shifted it to utilize ASyncTask. That being said, the part that I am working on right now, is reimplementing the local SQLite db: I need to get it to store the logged in user's email etc, so that I can display this info on the screen. In addition, I have a page where the user can submit more information (age, nationality, etc) and the app will then store that data on the SQL server with the rest of their account info

So as I said, I'd be glad to share this code with you, but it's not exactly what you're looking for, as its a network database rather than a local SQLite db. It will have the SQLite db aspect, I just haven't finished that portion.

Finally, I do suggest AddressBook from your Deitel book, it really helped me out.

Davek804
  • 2,804
  • 4
  • 26
  • 55
  • Thanks for the reply. You're right, I'm not interested in a network database, but the local one, once you get it up and going, would help me greatly since it sounds just like what I'm doing. Each Player object in my app keeps track of things just like your app keeps track of age, nationality, etc. I tried to use the Deitel book for my app, but the way they explain it is "well we used a ListView and it automatically does everything because it's a ListActivity." So, it doesn't help me for my app. The book is good in a lot of ways, but bad in others, like this. – S Fitz Apr 02 '12 at 08:22
  • I agree. I'll try to post some more info about how I work with my local SQLite dB later on today, would you rather me edit this post or drop another reply? – Davek804 Apr 02 '12 at 16:30
  • It's probably easier because of the character limit to drop another reply? Whichever works, I'm desperate. – S Fitz Apr 02 '12 at 18:28
0

AsyncTasks are pain when it comes to configuration change (e.g. rotation of phone) and it's easy to create memory leaks. You should rather use a Loader. Don't mind the name, it can be used to write data as well - basically for every operation done in background. More specifically I would use an AsyncTaskLoader and in loadInBackground() method I'd put a database helper to SELECT or INSERT the data with it. Database helper should be an extension of SQLiteOpenHelper and it sould be instantiated only once so I would suggest putting it in the application context and grab the reference from there.

Hope it helps.

Michał Klimczak
  • 12,674
  • 8
  • 66
  • 99
  • Hi, thanks for the reply. I'm not worried about the phone rotating since it's set to portrait in the manifest. I'm more worried about where to put each piece of code. I know where to "save" things, but I don't know how to reload everything every time the user restarts the app, particularly with regards to onCreate(), onResume(), onStop(), etc. These methods I still don't completely understand when it comes to saved information, restoring state, etc. – S Fitz Apr 02 '12 at 08:18
  • Also, I checked out the AsyncTaskLoader docs you linked to. It says they were introduced in Android 3.0 so I can't use them since my app (and phone) are for API 8+. – S Fitz Apr 02 '12 at 12:43
  • 1
    You can! They're in the Compatibility Package, so you can use them for API 7+ if I remember well – Michał Klimczak Apr 02 '12 at 13:37
  • Cool, I did not know that is a thing, haha. I'll look into it. I still don't know about reloading all the info though. Saving state is lame. :( – S Fitz Apr 02 '12 at 14:03
  • What do you mean by reloading info? If you save the data in SQLite it persists between user sessions. All you need to do is to start the loader in e.g. `onCreate()` and then listen to when the data is loaded from database with LoaderCallback methods (an example on how to do this here: http://developer.android.com/reference/android/content/ )AsyncTaskLoader.html). Once you have the data - display it – Michał Klimczak Apr 02 '12 at 14:12
  • I'm going to experiment with this and see how it works. I'm still iffy on the whole subject. – S Fitz Apr 04 '12 at 11:04