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
Related
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.
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!
when I made an SQLite database for an Android app I made a helper class that does not extend any other class. it is just to set up the database, in this example it is called PlayGame. it has a private class inside it shown here:
private static class DbHelper extends SQLiteOpenHelper
when I use the database for any reason in the main UI class called SQLiteExample I have to create an instance every time like this,
PlayGame entry = new PlayGame(SQLiteExample.this);
entry.open();
entry.createEntry(name, hits);
entry.close();
I am creating many instances of this database class called PlayGame in the other Activity class. Like in most all of my methods have to make instances to do the required function like read information into or out of the database.
so I read in best practices of android documentation that creating instances is heavy on memory and is best avoided. If this is the case is there a better way to do this? and does my example look like a bad use of memory?
Use singleton for every instance you want to use. instead of create a new instance of any object you will use the same object.
http://en.wikipedia.org/wiki/Singleton_pattern
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...
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.