Parse.com: read and write a table in the cloud

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 ParseObjects. 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 deletes 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.

Source code in FirstApp.zip

  1. 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.
  2. 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.
  3. activity_main.xml. The RelativeLayout contains a ListView and a TextView.
  4. edittext.xml consists of an FrameLayout containing an EditText. The FrameLayout and EditText will be inserted into a dialog.
  5. menu_main.xml is the options menu displayed by the Activity when you tap the three white dots in the action bar.
  6. strings.xml
  7. AndroidManifest.xml has two <uses-permission> elements:
    1. android.permission.INTERNET
    2. android.permission.ACCESS_NETWORK_STATE
  8. build.gradle (Module: app) has two extra dependencies:
    1. compile 'com.parse.bolts:bolts-android:1.+'
    2. compile fileTree(dir: 'libs', include: 'Parse-*.jar')

How I created the project

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.

How you can run this project

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.

Parse documentation

  1. Quick start.
    1. For product, select Data.
    2. For environment, select Mobile.
    3. For platform, select Android.
    4. For language, select Native (Java).
    5. For type of project, select Existing Project because we are going to add Parse to a project that we already created using Android Studio.
    These selections will lead you to Install the SDK for an existing project.
  2. Android Guide
    1. Getting Started
    2. Objects
    3. Queries
    4. etc.
  3. API Reference, including the following Java classes.
    1. Class ParseObject has the method saveInBackground, whose parameter is a SaveCallback “listener” having the method done. See also The ParseObject in the Android Guide.
    2. Class ParseQuery has the method getInBackground, whose second parameter is a GetCallback “listener” having the method done.
    3. Class Parse
    4. etc.
  4. Dashboard.

The dashboard

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.

Things to try

  1. Replace the deprecated TwoLineListItem with a vertical LinearLayout. This was one of the exercises in CursorAdapter.