Android :: Sharing Cursor Between Activities
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
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 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
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
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
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
Mar 29, 2010
The structure of some of my activities is a simple ListView with a custom CursorAdapter. The cursor is created in onCreate() on the activity from a SQLite database. The problem is that querying the SQLite database can be quite slow at times with lots of data (and let's assume I've already optimized the sql query as much as possible). Because it occurs in onCreate() on the UI thread, I get ugly black screens when opening the activity, which sometimes turn into ANRs, on a slow phone like the G1. I want to load the cursor in a background and show "Loading.." on screen while doing so. I saw AsyncQueryHandler used extensively in the framework, but this seems a solution geared more towards Content Providers and not application-local SQLite databases. I then thought of trying to load the cursor in a background thread, but realize that this might be problematic, as the CursorAdapter should be instantiated in onCreate() and should take a cursor as a parameter. The latest thought I had was to instantiate an empty MatrixCursor in onCreate() and pass that to the cursor adapter, while kicking off a thread/TimerTask to query the database. Then, on database cursor load, call cursorAdapter.change Cursor to the properly filled cursor. This doesn't seem very elegant and seems quite wasteful, however.
View 10 Replies
View Related
Mar 21, 2010
I get records from the system by quering a ContentResolver. I maintain the order of the items in the database. So I want to display the items in the order taken from my database.
How do I merge these two informations?
I am looking after an alternative way now. As what I ideally want is:
Get order of contacts by a custom order held in my database (this involves joining CR with my DB cursor, and doing an order by, later seams it's not possible with CursorJoiner) but there is more, if the join is not unique I want to sort by contact's name as last measure
Which is impossible using Cursor and Joiners, because of the missing feature of order bys, also I need to return a Cursor, as I will be using the in an ExpandableList
Also this translated to TSQL it would look like
select * from contactsdata
left join category on contactsdata.catid=category.id
order by category.pos asc, contact.display_name asc
So I am looking now after an alternative. I have in mind to load in a temporary DB table all data from CR, then do the query on the temporary table where I can join tables and do order bys? How does this sound to you?
View 1 Replies
View Related
Sep 19, 2012
I have my Google account also setup on my wife's phone so we can share purchased apps. However, everything else is shared too. For example, even though I have sync for everything on my account turned off, my calendar still showed up on my wife's phone until I disabled it in the calendar app. Also, I can't remove my Google Talk account from her phone so my messages show up on her phone.
This is on a pair of stock Samsung Galaxy S3 phones.
View 2 Replies
View Related
Jun 30, 2010
how to call BarCodeScanner, and return the value to a field.so now, i have a toast that says "successful scan" and then i want to pass the result to a new activity. when i comment out my intent, everything works (minus the passing of data/switching of screen, obviously) but when i run my project as is, it FC's no errors reported by eclipse in code or xml. any insights?
View 2 Replies
View Related
May 6, 2009
I am seeing the exception in 'adb logcat'.But I don't know if it is caused by my application or android platform.Can you please give me any idea how to troubleshoot this exception?
View 3 Replies
View Related
May 17, 2010
How do I get the row ID from a Cursor?
View 2 Replies
View Related
Jul 22, 2010
When I plugged my phone into my computer (Mac) to move music on to it it came up with the option to charge only, HTC sync, Disk drive, and share Internet. I know that with Sprint you can pay for the Mobile hotspot app, which in my opinion is way to expensive, so I was wondering if the USB sharing is free or if it is bundled with the mobile hotspot add on.
View 2 Replies
View Related
Jun 7, 2010
When connecting through USB my HTC Desire, I am sometimes asked if I want to share the Internet. The fact is that it would try sharing HTC Desire's internet to my computer, but it has here no interest, as the opposite is most current for me. Is there a reverse implementation of this as an Android GUI?
View 2 Replies
View Related
Nov 22, 2009
I was wondering if anyone knew a good GPS sharing app for the Droid so that my family can follow along with me on my trip back from college. I take a bunch of different people back with me, and figured if there was an easy way to share my GPS location (constantly refreshing) on a website for their family along as well.
View 10 Replies
View Related
Oct 18, 2009
I've got this piece of code where I count words in a dictionary and then I try to query them:
CODE:.............
The SELECT_BY_LENGTH is defined this way: private static final String SELECT_BY_LENGTH = "select word._id, word.word from word where length(word.word) between ? and ? ";
And the count works perfectly but the cursor.move(1) always returns false. I've tried to get the column count and responds with the correct count and the column names map too. I mean that the method getColumnIndex works too but I can't get any data from the cursor. All such methods fail. Am I doin' something wrong? I'm using android 1.5.
View 5 Replies
View Related
Apr 24, 2009
I call ContentResolver's query method to get a Cursor, and I would like to update the title of that row to 'new title' how to do it?
CODE:...................
View 4 Replies
View Related
May 17, 2010
How do get the row ID from a Cursor?
View 5 Replies
View Related
Jun 16, 2009
I have a Cursor points to a Contact. How can I get an url built from that Cursor?
I need to know that because from here: http://developer.android.com/guide/topics/providers/content-providers.html
I need to have an url so that I can build a phone uri, like this: phoneUri = Uri.withAppendedPath(uri, People.Phones.CONTENT_DIRECTORY);
and I can query all the phone numbers for that contact.
View 1 Replies
View Related
Jun 25, 2009
I have two tables I want to (inner) join, Contacts.People and Contacts.ContactMethods. The result will be displayed in a ListActivity. After looking into this, it appears that I should use CursorJoiner. So the code would look something like this: So my question is, how to I make a new cursor from the first two?
View 2 Replies
View Related
Nov 19, 2010
I have a query that is making use of GROUP BY so that the results are grouped by a date column. My question is this:
When I move through the cursor to get the results of the query, how can I get the multiple items associated with each group into my vector? For example, I want to get BOTH "Item X" and "Item Y" for October 16, 2010. Currently, I get them each separately.
Here is my code:
CODE:..........
So, when I get the results from my Vector, I will get:
October 12, 2010
- Item X
October 12, 2010
- Item Y
And I want:
October 12, 2010 - Item X, Item Y
Yes, I do have to change my Vector to support multiple items, but hopefully you get what issue I'm facing...
View 1 Replies
View Related