How can I use a db through different Activities in android?
I created an SQLite database in my Android project to store information
about homes using SqliteOpenHelper.I have two Activities.One is a list
activity containing a list of all homes, and one that is started by the
listActivity to add a new home. My question has to do with the usage of
the db. Which is better? Have an instance of the db in both activities? Or
have a static instance of the db in only one activity and use it in every
other activity that need acces to it? What is the better way to use a db
through different Activities?
public class HomeDatabaseHandler extends SQLiteOpenHelper{
//Database Version
private static final int DATABASE_VERSION = 1;
//Database name static value
private static final String DATABASE_NAME = "homeManager";
//Table name
private static final String HOMES_TABLE = "homes";
//more code here for adding creating etc...
}
public class MainActivity extends ListActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Here is my db for populating listView
HomeDatabaseHandler db = new HomeDatabaseHandler(this);
}
//in Activity to add home
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_done:
//more code here to get values from views
//create new db to add values
HomeDatabaseHandler db = new HomeDatabaseHandler(this);
//method to add home
db.addHome
}
}
This is how it looks right now, creating to instances of the Handler and
adding. So what do you think?I feel this is not the best way...
No comments:
Post a Comment