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!
Related
Hey guys so i want to use getReadableDatabase() in my main activity but the problem is that it is already extending appCompatActivity() and i need it to do that for my code. But now since i cant extend 2 things at a time ,i cant extend SQLiteOpenHelper and that leads to not being able to use getreadable. Can anyone help me with this issue?
You need to define create a second class that extends SQLiteOpenHelper and define methods within there that use getReadableDatabase(). You can set up SQLiteOpenHelper as a singleton so that you can access a single instance of the class from anywhere.
See http://blog.anorakgirl.co.uk/2018/01/refactoring-sqliteopenhelper-so-it-isnt-a-mile-long/ for how you can structure your SQLiteOpenHelper class.
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!
I am implementing two fragments to display different views on the same table retrieved from a ContentProvider:
1/ A class which extends ListFragment to provide the data in a list
2/ A custom fragment class which itself contains a Google Maps v2 fragment
The list fragment is a standard implementation using a loader and SimpleContentAdapter, so when the name of a list item is edited in a seperate activity, the change is reflected back in the list.
I want to do something similar with my custom fragment so that map markers are created and updated as the data changes.
What is the best practice for doing this?
It appears that using a SimpleCursorAdapter would not work since it returns View objects whereas I need to create/update Marker objects and these are not derived from View. I'm thinking along the lines of creating my own CursorAdapter class, implementing ContentObserver on my map fragment container and registering this with the CursorAdapter. Is this best practice? Does anyone know of any sample code demonstrating best practice?
Sorry if this is a stupid question - I'm new to Android programming so not completely clear on the notification architecture - but I have tried reading around the subject to no avail.
Okay, I've worked out the answer to this. There is no need for a CursorAdapter or a ContentObserver. The steps are as follows:
1/ Create Fragment-derived class (in my case derived from MapFragment)
2/ Implement LoaderCallbacks on this class
3/ Call getLoaderManager.initLoader() in onCreate() override of this class
4/ Create a new CursorLoader() in your LoaderCallbacks.onCreateLoader() override
5/ In LoaderCallbacks.onLoadFinished(), call code to iterate round the cursor and populate your view. But DON'T CLOSE THE CURSOR OTHERWISE YOU WON'T RECEIVE FURTHER UPDATES!!!
My problem arose from adapting some sample code in which the cursor.Close() was called once the list had been populated. I guess good practice is to save the cursor in a private data member and close it when LoaderCallbacks.onLoaderReset() is called.
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
I am trying to understand what does LoaderManager do. Can anyone share an example with it? Must I use them when I create a cursor? If not how should I use? A simple example is very appreciated.
Simply stated, the LoaderManager is responsible for managing one or more Loaders associated with an Activity or Fragment. Each Activity and each Fragment has exactly one LoaderManager instance that is in charge of starting, stopping, retaining, restarting, and destroying its Loaders.
There is a pretty extensive and in-depth blog post on the LoaderManager... check it out here:
Understanding the LoaderManager (part 2)
In simple words:
LoaderManager load the data in background and also look for the changes in it. It provides a simple Api like structure that we don't need to look and observer the data manually.
Examples:
In <android-sdk>\samples folder search for LoaderCursor.java and LoaderThrottle.java this are the to example using CursorLoader class
Just in case if some one is looking for an example of LoaderManager with the custom AsyncTaskLoader look here.