Android how to use CursorLoader with multiple tables? - android

I have my database and ContentProvider all set up. I'm trying to get all the data from my database using a CursorLoader, and I'm wondering how exactly I can do that for multiple tables. Do I create a separate fragment activity for each table and implement the cursorLoader? And when do these activities get invoked so that I can use them? Should I instantiate them within the MainActivity?
Thanks for any help!

Related

How to pass SQL database data from one activity to another?

I have a simple application consisting in a contacts database (using a SQLiteDatabase class).
In my main activity layout, there is a "View Contacts" button. When pressed, I create a new activity (let's call it "ContactsActivity") in order to show a LinearLayout with the contacts data.
My question is: how can I give ContactsActivity access to the database?
Do I pass some data from the main activity (which I think is not an option)?
Do I just kind of open the database from this activity?
Do I use the same method that I used for the main activity (I mean, using a SQLiteOpenHelper and so)?
I don't know if I am giving enough information; otherwise, just ask me and I will provide any necessary info.
Thank you.

Use a cursorloader from another activity

Is it possible to make use of - have access to a cursorloader that is implemented from another activity? That is to have a helper class that
makes all the calls to a cursorloader, and this cursorloader being accessed from different activities. If so, how this can be done? I know that in order for this to work every activity should implement its own cursorloader.
As pskink pointed out: "create a class that implements LoaderManager.LoaderCallbacks and use it wherever you want". Thanks!

Access SQLite db from non-activity class

I have three classes,
Activity - displays the listview with my single_list_item.xml
SQLiteOpenHelper - I'm currently running my queries here.
My activities and my SQLiteOpenHelper talks together well. However I'm not sure how to run a query from my third class
BaseAdapter - This one is not in my manifest, and with my current skills I can't access the db from this one - as I normally do from activity classes.
I want to hide a textview in my single_list_item.xml depending on the value from my query(the query only returns 0 or 1). I can easily hide it or show it statically by defining tv.setVisibility(View.GONE) etc. - But I want this one to be active or not depending of the usersettings.
Any help is greatly appreciated.
A common approach is to create a singleton, that is first initialized with a Context. That holds the SQLiteDatabase, and has all the data specific methods that you use from your application.
Thus, from your BaseAdapter, you call MyDBSingleton.getInstance().getData()
Be sure, that the singleton class keeps an ApplicationContext, and not a reference to an Activity/Service, as that could cause leaks...

How to handle number of activities with SQLite?

I have one Activity, I created class called DatabaseHelper that extends SQLiteOpenHelper,
now when I want to use it (has I see in many simple examples) I need to have new SQLiteDatabase Object & implement insert, update, delete... in my Activity code.
What is the right design if I want to work through database with number of activities and to create DatabaseHelper just once, for all the activities will work with it.(and not have duplicate code.
simply: what is the right classes SQLite structure for number of Activities
Thank you!!
Just use one DatabaseHelper throughout your application, there's no profit in repeating yourself.
Make your DatabaseHelper a singleton across your entire application's lifecycle. Check out my blog post on the topic:
Correctly Managing Your SQLite Database

best way to share the same database among all activities

I am using a database that I need to access in all activities. So I created a class MyDBAdapter with all the methods open, create, getData etc...
To access the DB I see two different ways:
a. In each activity do I now write: MyDBAdapter db = new MyDBAdapter();
which means a new DBAdapter would be created in each activity. Each would open the same DB and have to close it. This also means that the same DB might be opened by several activities - is that OK? Or do I manually ensure that the DB is closed everytime I switch activities?
or
b. create only one instance of the DBAdapter in the first activity and pass it everytime to the next activity using putExtra("Task", x);
Then the DB gets only opened and closed once in the very first activity.
Which one is a better approach?
Thanks very much.
I prefer content provider to share data among activities.
Even though its mainly aimed to share among applications, It can be used inside our single app.
If we use content provider, there is no worries of closing and locking of db.
Content providers implement a common interface for querying the provider and returning results — as well as for adding, altering, and deleting data.
It's an interface that clients use indirectly, most generally through ContentResolver objects. You get a ContentResolver by calling getContentResolver()
Check this Simple Content Provider for db operations
Generally you have to close the database, right after you finish working with it (manually call db.close()). This mean that if you use your database in multiple activities, you have to close it every time after you have done using it. That said, it's up to you if you will make calls MyDBAdapter db = new MyDBAdapter(); in every activity or you will make a static reference to it. After all, if you have some local variable MyDBAdapter db in some code block, it will be garbage collected at some point.
Also generally speaking, you don't want your database operations in your Activities, because this way they will be executed on the UI Thread, blocking it, until they finish their db operation. You might want to use AsyncTask for your db operations which will create a separate Thread for db operations (and handle it for you). Here's a great tutorial on using AsyncTasks.

Categories

Resources