Android :: Sharing SQLite Database Between Activities
Jun 24, 2010
I'm in the middle of writing myself an app and have run into a problem concerning the use of the same SQLite database within multiple activities.
I have one activity that is used to input and store data and multiple activities that need to then read that data. Within the first activity, it has no problem inputting the data into the database, but when I go to have the other activity read the data, it tells me the database is empty. Obviously I'm doing something wrong in my handling of this shared database.
Right now, I have a DataHelper class that handles everything. In each activity, I create a DataHelper object which then calls the openOrCreateDatabase() method. I then close the database upon exiting the activity. Obviously this isn't the right way to do it.
So, my questions is, what is the correct way to set up an SQLite database in an Android application and then access it in multiple activities?
View 7 Replies
Oct 12, 2009
Can two or more Android Activities open an sqlite3 database for write?
I have two Activities that need to insert data into the same sqlite database. When the second Activity calls SQLiteOpenHelper.getWriteableDatabase() an IllegalStateException is thrown with the message "SQLiteDatabase created and never closed".
I've been able to avoid the Exception by making my database object a singleton but I'm thinking there must be a better way.
View 2 Replies
View Related
Apr 27, 2010
I have been trying to find a discussion on the best way to handle a common sqlite database which is shared by multiple Activities (or multiple Activities and Services). This is all in the same application.
It seems that if each Activity has:
CODE:.........................
View 14 Replies
View Related
Jul 20, 2010
I am developing an app which requires me to write to an SQLiteDatabase in one activity and access it from another activity . I am facing a problem implementing this. Any suggestions/ideas as to how we can share the database across multiple activities?
View 2 Replies
View Related
Sep 20, 2010
I am creating an app which allows for many different Activities to be started from a TabActivity(up to ~25). Most of the activities require data from the sqlite database, so when onCreate is run, an AsyncTask creates an SQLiteOpenHelper object(which will open a readable/writable database), runs a query, data is retrieved, and everything is then closed.
I was just testing messing around to see if i could break something, so i added every Activityto the TabActivity's TabHost. I then started mashing each tab as quickly as possible.
I noticed that very quickly i began to see in the LogCat: Caused by: android.database.sqlite.SQLiteException: database is locked: BEGIN EXCLUSIVE; and the app proceeded to die.
Typically there will only be about 4-6 tabs(i can just limit the user anyway) for the TabHost. I haven't been able to break anything with a small amount of tabs to mash, but i am still worried that maybe i am accessing the database in a poor way.
How can i prevent my SQLiteDatabase objects to cause a lock?
If i create a ContentProvider will that eliminate the possibility of database locking?
Do you have any suggestions for changes I could make for accessing data from an SQLiteDatabase?
I ended up taking the approach of using the Application class and storing 1 SQLiteOpenHelper and trying my best to keep it synchronized. This seems to be working great - i put all my 25 activities in the TabHost and mashed away on them with no errors.
I am calling ((SQLiteDbApplication)getApplication()).setDbHelper(new DBHelper(this, Constants.DB_NAME, null, Constants.DB_VERSION_CODE)); method(shown below) in every onCreate() in my activities
Any further suggestions to this approach or to the changes i made using this Application class?
CODE:...................
View 3 Replies
View Related
Apr 7, 2010
I was playing a bit with adapters cursors and activities. Let's assume i have a cursor containing a query I've made (it doesn't matter if it's to DB or content provider). Now lets assume i bind some of the data (first two rows) to a list activity. Every click on one of those items should open a new list activity (i pass on the cursor index in the activity bundle). Now the question is, which sharing method will be most efficient and right from code point of view ?
1. I thought of trying to send the cursor object in the intent itself (hoping it implemented runnable) but I'm not so sure it's that efficient since the query may contain many rows which will need to be parceled, and i the other intents uses only a few (say 4-5 columns of each row) it's a real waste.
2. Thought of trying to create the array list for my other list screen adapter and serialize it to the intent (again use serialize and intent to pass on the data) but again i fear for efficiency penalty.
3. my last solution was to use static cursor member in my first activity, with package permission, it will work and be relatively efficient, but I'm not so sure regarding memory efficiency and code structure, i'm not fond of using static variables in any case.
Is there another sharing method that i don't know off, if two activities share the same process and application, then they also share memory space, then it should be easy to pass variables from one to another, the question is what's the easiest way without serializing ?
View 6 Replies
View Related
Aug 10, 2010
I did some digging, but couldn't find a clear answer. I have an application with TabActivity as the main activity. I have some computing and network communication that needs to be done when user clicks on the big red jolly "DO IT" button. One tab hosts the form with "DO IT" button, while the other tabs display the results from computing (each tab displays different part of results). The computing is done as AsyncTask as it's supposed to be (afaik).
Now the question is, what is the best way to share the results between the tabbed activities? It can be done with ContentProvider + database from what I have read, although that seems like a bit too much for my needs. I have also considered an option to save the results to some cache file activities could read in onCreate, onResume etc and display the data. Are there any other convenient ways to share the data? (To make it more clear, the data aren't simple, so doing it through preferences etc is impossible.)
View 5 Replies
View Related
Mar 13, 2009
I was looking out for ways to share data between various activities that one may need to create for his applications . Using "Parcel" and Bundle one can do data exchange , but looks like this is a bit cumbersome process specifically if you have to share large array objects with too many fields.
As android.app.Application maintains global state for the application , we can use this class to store global data and access it across activities. Is this a good way ? Are there any implications on allocating large objects in the your class which extends from android.app.Application ?
View 6 Replies
View Related
Oct 7, 2009
I have an app with two activities.
Both activities among other functions have to perform the same calculation on data, so each activity include the same code to perform the calculation.
Is it possible to only have the similar code in a separate activity and be accessed by the other two activities instead of duplicating the code twice?
View 15 Replies
View Related
Nov 21, 2010
I think the following scenario is common, but I can't figure the way to implement it:
We have three Activities, and one database. We ask the user for an input to search in the database. The query should get none, one or may results.
If you get none you simple inform the user.
If you get only one, you show it in a new activity with the right view.
If you get many you show them in a list to let the user to chose the right one and then you pass that to the activity which with show the data in the case of one result only.
The problem is that, in order to know the number of results, you need to do the query (and get the cursor) in the first activity. And in the case you get more than one result you need to send the data (pass the cursor?)to the list acitivty. Doing the query again in the list activity can't be right, right?
I'm aware that you can share cursors by using a content provider, but as the activities are from the same application and the data is private (useless outside), don't see the point of making it avaliable to anyone else.
I read here around that you can crate a parcelable cursor and send it in bundle, but I'm not sure if that is the right use.
View 1 Replies
View Related
Nov 24, 2010
I have a Weather app with four Activities. The main/launcher activity is 'invisible' using...
android:theme="@android:style/Theme.Translucent.NoTitleBar"`
Aand is simply used to do a few checks (whether this is a new install, whether a network connection is available etc) before firing off one of the other Activities. The other Activities are UI-oriented - two simply display weather data pulled from a website and the third to provide a location 'picker' so the user can choose which area to show the weather for.
However, all four activities make use of a WeatherHelper object which basically does everything from checking for available SD card storage to maintaining preferences and pulling/formatting website pages.
So, my question(s)...what is the best way to have one instance of WeatherHelper which can be used by multiple activities and where/how are best to create it in my case?
I've been an OO programmer for a lot of years but I'm very new to Android and the design concepts - I've read a lot on the Android Developers site over the past weeks but I've stalled trying to decide on this.
View 2 Replies
View Related
Jul 2, 2010
I have an Android application composed by several Activities. Most of them need to check whether an active network is available or not:
public boolean isNetworkAvailable() {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
TelephonyManager tm = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
...
}
I would like to move that code to a helper class in order to avoid the need to write that code in every Activity, but calls to getSystemService are only allowed from an Activity. The other alternative is to move that code to a parent activity use inheritance but:
Every activity already extends from android.app.Activity
Some of my activities already extend from a common my.package.BaseActivity
(Activity <- BaseActivity <- XXXActivity)
What do you recommend in this case? Is there any other alternative?
View 1 Replies
View Related
Aug 23, 2010
I am working on a small android project where it is necessary to share some data amongst several activities and a service that runs in a separate process. I would just like to know what are my options in terms of sharing data? Application class? IPC? File-based? Broadcasts?
View 3 Replies
View Related
Jan 11, 2010
I have written my application logic in domain objects (to enable multiple user interfaces and porting to other platforms), and am now lookng at implementing Activities for the user interface.
Considering that each activity needs to serialise its state, what is the best way to ensure my domain objects are only serialised once?
View 1 Replies
View Related
Jun 22, 2010
I have two activities that both contain an identical context menu built programmatically using menu.add(int, int, int, CharSequence). They both use onCreateContextMenu and onContextItemSelected.
The first Activity contains a ListView, when the user long presses on an item the context menu appears for that item. The second Activity (a detail screen for each item in Activity 1) contains a button, when the user presses the button the context menu appears.
I'd like to share the code that creates the context menu between these activities. The only thing these activities share is the context menu, so it seems like a bit of overkill to create a superclass (e.g., ActivityOne extends ContextMenuActivity) that defines onCreateContextMenu and onContextItemSelected methods.
Is there a better way of sharing these methods between activities?
View 2 Replies
View Related
Sep 15, 2010
I want to share streams of a socket connection between the activities of a tab view. In particular the tab activity creates the socket and gets the i/o streams,so i want that tabbed activities use these streams to retrieve informations without reconnect to the server each time i switch from a tab to another. I know i can use the application class to have a global state, but i don't know how.
View 4 Replies
View Related
Jul 22, 2009
I have been using SharedPreferences to share data between activities. But I would very much like to find a better way. I would simply like both Activity1 and Activity2 to share Object1. Activity1 will create Object1 and then start Activity2. What is the smartest way to give Activity2 a pointer to Object1?
To summarize: Activities don't have constructors! How do I send data to them from their parent activity?
View 17 Replies
View Related
Jun 23, 2010
I'm sharing some variables accross activities by using a class like this :
CODE:..........
Then I'm using these variables anywhere across my multiple activites with ...if (Globals.hint2_stockview_valuation == false) {....}
Pretty basic and it was working fine untill ...
I introduced some webview stuff like this:
CODE:...........
And here's the News_Webview.class:
CODE:............
The problem now is that, when it "comes back" from this Activity, it looks like my Globals variables have disappeared ?
if (Globals.hint2_stockview_valuation == false) {
fires an error :
06-23 12:14:03.443:
ERROR/AndroidRuntime(2611): Caused by:
java.lang.NullPointerException
2 Questions then:
Should I use something else than this "Global" class to share variables across activities ? Is it just bad practice to do this ?? I know that I can use the preferences but I thought it was quicker to do it this way (no need to "read" the preferences everytime I start a new activity ...Should I "get back" my savedInstanceState in some way when my activity returns from the News_Webview.class ?
View 2 Replies
View Related
Aug 23, 2010
I'm trying to deploy an application with an existing SQLite database.I've been reading though the examples that are posted but they are always missing some part of the class. I feel like I'm trying to bake muffins but no one told me to use baking powder.Can someone post a full database helper class for depoying an SQLite database on Android? Edit : Delete old code because it doesn't work.
View 2 Replies
View Related
Apr 12, 2010
I'm currently developing a Field-Service application that stores data in the local sqlite database on an android device. At some point, usually after completing the data collection rounds, the local sqlite db is to be synchronized to a remote sybase db on the server.Any suggestions as to how this could be achieved or engineered as a solution? Or even better, are there alternatives to synchronizing data in such an application?
View 1 Replies
View Related
May 15, 2009
Anybody knows if we can share databases between applications? How can I access to a database from an A application to another B application? It is possible? What URI I would have to put?
View 4 Replies
View Related
Aug 11, 2009
What's the most convenient way to share a database across applications? For example, I want my application's dbadapter to query the android's native contact database so when a contact on the phone is deleted, similarly that contact is removed from my application. Should i use an attach statement to attach the table? Or would the better/more secure approach be to create a custom contentprovider query onCreate of my app to verify all contacts in my application still reside on the Contacts.db.
View 4 Replies
View Related
Feb 13, 2009
I'm new to Android, I just finished the Notepad Tutorials, where we can create SQLite Databases to save notes. So I wanted to know if it was possible to "import" my own SQLite database (not created with my App but with a 3rd-party software) to my project, and if the answer is yes, where should I save my SQLite databse and how can I have access to it.
View 9 Replies
View Related
Mar 25, 2010
I'm doing a application that will use a database with serialized object. The objects will be already serialized and the user will just display things. On a PC I would just make an XML but with android I'm not sure. Can I make an XML and "unpack" it when the user launch the application for the first time or should I do something else?
View 3 Replies
View Related
Mar 5, 2010
I put my database field in "assets" folder. And use the code from this blog to copy the database to "/data/data/my_packname/databases/", (This copy code i run it in the onCreate() method when i run this app) then use select * from ... to get data. But it gives me the exception: no such table. Someone told me that if i am attempting to copy the file in SQLiteOpenHelper's onCreate(), it's too late. So the copy file code can not copy the complete file. So i need to use adb or ddms to pull the database first? So, Anyone can teach me how to use my own databse?
View 3 Replies
View Related
Nov 18, 2009
I have a class like class My data{ String name; int data; Location[] locality.
View 8 Replies
View Related
May 23, 2009
I am new to android and I have a problem in creating a database.
public class database extends ListActivity {
/** Called when the activity is first created. */
private final String MY_DATABASE_NAME = "myCoolUserDB.db";
private final String MY_DATABASE_TABLE = "t_Users";
Context c;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> results = new ArrayList<String>();
setContentView(R.layout.main);
SQLiteDatabase mydb=null;
try
{
mydb.openOrCreateDatabase(MY_DATABASE_NAME, null);
} catch(Exception e){}
}}
When I run this code it throws a run time exception.
View 4 Replies
View Related
Mar 23, 2010
How do I delete the sqlite database exists in my android? There is a db behind my android qro and delete the same
View 4 Replies
View Related
Aug 2, 2010
public void delete(String id, String name, String phonenumber, String time, String message)
{
String[] args={id};
getWritableDatabase().delete("texts", "_ID=?", args);
}
It works fine, and deletes the specified row. However, I also want to implement it so that I can delete the most recent (earliest date) entry in the database, which should be at the top of the table. I know I can do this by altering the last 2 parameters of delete, but I cannot figure out what to change them to.
View 1 Replies
View Related
Jan 25, 2010
Is possible update the records from SQLite from my Android Application1 from other Application2 Android
View 4 Replies
View Related