sqlite - Android: When to bind controls to database -


when best time during activity bind controls (such textview) values in sqlite database? thought remember reading should in onstart() of examples have seen set values in oncreate().

here example of code:

//i think goes in oncreate mydb db = new mydb(); db.open(); textview tvtextview; tvtextview = findviewbyid(r.id.tv1);  //i'm not sure whether put in oncreate(), onstart(), or onresume() tvtextview.settext(db.getmytext()); 

normally mydb , textview variables whole class.

i suggest using laoders in fragment support library. example shows how use loaders (using ones in hc api) load cursor contacts db , update list adapter when first result , updates:

http://developer.android.com/resources/samples/apidemos/src/com/example/android/apis/app/fragmentlistcursorloader.html

using fill in own data same, instead of setting in adapter when cursor can pull data want out , set in fields.

article on how support library version of these:

http://android-developers.blogspot.com/2011/03/fragments-for-all.html

i recommend preferred, modern way load data cursors , other sources. takes care of lot of things make sure doing things best way: query asynchronously ui doesn't block waiting it, monitors data changes , gives clean way update new cursor when there change, takes care of integrating activity lifecycle, propagates previous data across activity instances when activity destroyed , new instance created due config change, etc.


Comments