Now this tutorial dependent on Part1. So if you did not read Part1 please read that first.
Refer to Part 1. There i already give motivation. I think that motivation is enough for Part 2. We will enjoy part 2 becase there only we see application working.
0. OK Guys. Now we are confident about content providers. Now we are creating an app in which we use our ContentProvier.
Basic theme of our app shown below.
1. We have two edit fields in which we get user Name and Age, as we know we have Info Table as discuss in Part1.
2. Three buttons for three operations, which are insertion, deletion and updation. One more operation Query which will explain in 3 tag.
3. A list where we use Query operation and get all data and show that in our list.
And all these operations performed by using our ContentProvider.
2. Now we explain little our app code, So we easily know what things doing in background.
First we created a new activity class with name MainActivity and also added that in manifest.
3. Now i am showing MainActivity code in next image. Guys i am showing as Collapsed code. Because code is little bit lengthy. But you can easily learn. Code available on Github.
Here i only tell you about LoaderCallbacks<Cursor> and Cursor Loader Implemented Methods.
Basically thetre are three methods in Cursor Loader. OK now i will try to explain as simple as possible and as fast as possible.
First Term: Cursor -> When we query to Database it always return an object that is called a Cursor.
Second Term: Cursor Loader – > An API which handle our Cursor Loader procedure. Means that API know how to use the Cursor.
Now three methods.
In first method we are Creating Cursor Loader . And if you see we are given Second argument of our ContentProvider URI.
In second method when our query method load all data and give us a Cursor that method is called. And we got our data as Cursor. And in this method we use cursor for our List Data.
In third method we are only reseting our list data when LoaderManager call this method.
Now if you feel any ambiguity in CursorLoader please consult with below links. I will try to make a seperate tutorial for CursorLoaders in future. 🙂
Android Cursor Loader Documentation
Android Cursor Loader Training
Now practical example start.
Now the above image is the first screen which is showing no data.
Now here in above image, I only added new data and list automatically refresh. We did not add any notify call on ListAdapter. Because, now any change in list data is handled by ContentProvider. In simple words, ContentProvider is responsible to refresh our List Data. We did not need to take any tension of new data, removing data or updating data. Now i am showing one last image belowin which we update and delete data.
OK Guys now we complete our ContentProvider. I tried my best.
Now probably my next tutorial is will be on UnitTesting of ContentProviders.
Code available at : Githubby