Use this app the same way we used
Sqlite.
Each person in the table is an object of class
ParseObject
,
stored in the cloud.
This class is a
Parse.com class,
not an
Android class.
For each
ParseObject
,
we display four fields:
the name,
object
id
(which is a garbagy-looking string such as xoOwliy0tK),
creation
date and time,
and
updated
date and time.
I added the name field myself;
the other three fields are built-in.
Select Reset to erase all the rows and re-create the original four: Moe, Larry, Curly, and Shemp. Since the database is in the cloud, two or more people can run this app simultaneously. Select Refresh to see if anyone else has changed the rows in the cloud. (Later, we’ll do the refresh automatically.)
Two laws:
That’s why the work of downloading fresh rows and displaying them
is done by an object with two methods.
The
doInBackground
method of an
AsyncTask
object is executed by a background thread.
When it is finished, it can transmit information to the
onPostExecute
method of the
AsyncTask
,
which is executed by the UI thread.
Our subclass of
AsyncTask
is named
RefreshTask
,
defined inside the
MainActivity
class.
(We saw our first subclass of
AsyncTask
in
Offer.)
Its
doInBackground
downloads all the rows from the cloud and stores them in a
List<ParseObject>
,
which is sort of like an array of
ParseObject
s.
When
doInBackground
is done, it transmits the
List<ParseObject>
to
onPostExecute
,
which inserts the
List<ParseObject>
into an
adapter
and inserts the adapter into the
ListView
.
Note that a
RefreshTask
makes no change to the data in the cloud.
To execute the individual CRUD operations,
we create five anonymous subclasses of class
RefreshTask
.
The subclasses do make changes to the data in the cloud.
For example, we create a subclass of
RefreshTask
.
when the user deletes a row.
It overrides the
doInBackground
method of class
RefreshTask
with a new
doInBackground
which
delete
s
a row from the cloud,
and then calls the overridden
doInBackground
to get a fresh list of rows.
(Even though the original
doInBackground
is overridden,
it is still there and can be called by saying
super.doInBackground
.)
The
onPostExecute
method is not overridden,
so it’s still there too.
MainActivity.java
.
Following the instructions in
Connect
your app to Parse,
onCreate
calls
Parse.enableLocalDatastore
and
Parse.initialize
.
onCreate
makes the
ListView
touch-sensitive,
and then executes a
RefreshTask
object in the background to fill up the
ListView
.
PersonAdapter.java
defines a subclass of
BaseAdapter
.
(For other apps where we defined a
subclass of
BaseAdapter
,
see
Adapter
and
GridView.)
The
PersonAdapter
contains a
List<ParseObject>
read from the cloud.
The
PersonAdapter
creates a
TwoLineListItem
view for each
ParseObject
in the
List<ParseObject>
.
See
simple_list_item_2.xml
.
activity_main.xml
.
The
RelativeLayout
contains a
ListView
and a
TextView
.
edittext.xml
consists of an
FrameLayout
containing an
EditText
.
The
FrameLayout
and
EditText
will be inserted into a
dialog.
menu_main.xml
is the
options
menu
displayed by the
Activity
when you tap the three white dots in the
action bar.
strings.xml
AndroidManifest.xml
has two
<uses-permission>
elements:
android.permission.INTERNET
android.permission.ACCESS_NETWORK_STATE
build.gradle
(Module: app)
has two extra dependencies:
compile 'com.parse.bolts:bolts-android:1.+'
compile fileTree(dir: 'libs', include: 'Parse-*.jar')
Create a Parse account if you have not already done so. You’ll have to supply your email address, a secret password, and a name for your first app. Name it FirstApp.
Go to
Install
the SDK
for an existing project
and press the big blue Download the SDK button.
Double-click on the resulting
Parse-1.9.4.zip
file in your Downloads folder.
Open the resulting
Parse-1.9.4
folder and note that it contains a file named
Parse-1.9.4.jar
(“Java archive”).
Create a project named
FirstApp
in Android Studio.
File → New → New Project…
In the drop-down menu at the top of the Android Studio
project
view,
select Project Files instead of Android.
Open the Project Files as far as
▼ app ▼ app ▶ build libs
Copy and paste the
Parse-1.9.4.jar
file into the
libs
folder.
Then select Android again in the drop-down menu.
Your
build.gradle
(Module: app)
file already contains
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' }
Add two more dependencies:
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:22.2.1' compile 'com.parse.bolts:bolts-android:1.+' compile fileTree(dir: 'libs', include: 'Parse-*.jar') }
Then pull down
Tools → Android → Sync Project with Gradle Files
Add the two
<uses-permission>
elements to
AndroidManifest.xml
.
Select the name of your app in the drop-down menu in
Connect
your app to Parse
and paste the resulting code
(the calls to
Parse.enableLocalDatastore
and
Parse.initialize
)
into the
onCreate
method of the
MainActivity
.
Just download the
FirstApp.zip
file, unzip it, and open it in Android Studio.
You don’t have to create a
Parse account.
Two or more people can run this app simultaneously
on several Android devices or emulators,
but for the time being you will have to select Refresh from the
options
menu
to see what the other people have typed.
ParseObject
has the method
saveInBackground
,
whose parameter is a
SaveCallback
“listener”
having the method
done
.
See also
The ParseObject
in the
Android
Guide.
ParseQuery
has the method
getInBackground
,
whose second parameter is a
GetCallback
“listener”
having the method
done
.
Parse
To see the database stored online at
www.parse.com
,
point your browser at the
dashboard.
Select the name of the app
(FirstApp
)
from the “Select an App” drop-down menu.
Click on the ⚛ Core tab.
On the right side, click on the Refresh arrows
(a pair of counterclockwise blue arrows).
ACL stands for Access Control List.
TwoLineListItem
with a vertical
LinearLayout
.
This was one of the exercises in
CursorAdapter.