Android Tutorial Android Tutorial Part IV

View the Project on GitHub FatFractal/hoodyoodoo

Update the Client Code

In order to use this new extension method, you need to do add a matching StatsObject to your project.

public class StatsObject {
	private Integer m_totalUsers = 0;
	private Integer m_totalCelebrities = 0;
	private Integer m_totalRatings = 0;
	private Integer m_yourRatings = 0;

	public StatsObject() {}

	public Integer getTotalUsers() {return m_totalUsers;}
	public Integer getTotalCelebrities() {return m_totalCelebrities;}
	public Integer getTotalRatings() {return m_totalRatings;}
	public Integer getYourRatings() {return m_yourRatings;}

	public void setTotalUsers(Integer param) {m_totalUsers = param;}
	public void setTotalCelebrities(Integer param) {m_totalCelebrities = param;}
	public void setTotalRatings(Integer param) {m_totalRatings = param;}
	public void setYourRatings(Integer param) {m_yourRatings = param;}
}

You can then call the extension using the standard CRUD url pattern.

statsObject = ff.getObjFromUri("/ff/ext/Stats");

We also need to update TopCelebActivity to use our new StatsObject class. First, add in the additional instance variables we will be using:

private TextView m_totalUsersValueTextView;
private TextView m_totalCelebritiesValueTextView;
private TextView m_totalRatingsValueTextView;
private TextView m_yourRatingsValueTextView;

Next, we need to add a new method to retrieve the data:

public void getStats() {
	if(!ff.isLoggedIn()) {
		AlertDialog alert = new Hoodyoodoo().loginDialog(this, "You must be logged in to retrieve your personal stats.");
		alert.setOnDismissListener(new DialogInterface.OnDismissListener() {
			@Override
			public void onDismiss(DialogInterface dialog) {
				if(ff.isLoggedIn()) getStats();
			}
		});
		alert.show();
	} else {
		new AsyncTask() {
			@Override
			protected StatsObject doInBackground(String... params) {
				try {
					StatsObject statsObject = ff.getObjFromUri("/ff/ext/Stats");
					return statsObject;
				}
				catch (FFException e) {
					e.printStackTrace();
					return null;
				}
			}
			@Override
			protected void onPostExecute(StatsObject statsObject) {
				if(statsObject != null) {
					m_totalUsersValueTextView.setText(statsObject.getTotalUsers().toString());
					m_totalCelebritiesValueTextView.setText(statsObject.getTotalCelebrities().toString());
					m_totalRatingsValueTextView.setText(statsObject.getTotalRatings().toString());
					m_yourRatingsValueTextView.setText(statsObject.getYourRatings().toString());
				} else {
					System.out.println("getStats did not retrieve a StatsObject");
				}
			}
		}.execute();
	}
}

Now that we have our statsObject, let’s add in the UI components to display the results. To do this, we add additional TextViews for both the headings and the data as follows to top_celeb_layout.xml:

  1. Add two TextViews that will display “Total Users” and the data from totalUsers.
  2. Add two TextViews that will display “Total Celebrities” and the data from totalCelebrities.
  3. Add two TextViews that will display “Total Ratings” and the data from totalRatings.
  4. Add two TextViews that will display “Your Ratings” and the data from yourRatings.

Add the following code to the end of your layout file, within the TableLayout element:


<TableRow
  android:id="@+id/totalRatingsTableRow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  <TextView
      android:id="@+id/totalRatingsTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="10dip"
      android:gravity="right"
      android:text="Total Ratings"
      android:textAppearance="?android:attr/textAppearanceMedium" />
  <TextView
      android:id="@+id/totalRatingsValueTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dip"
      android:gravity="left"
      android:text="Ratings Count"
      android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
  android:id="@+id/totalUsersTableRow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  <TextView
      android:id="@+id/totalUsersTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="10dip"
      android:gravity="right"
      android:text="Total Users"
      android:textAppearance="?android:attr/textAppearanceMedium" />
  <TextView
      android:id="@+id/totalUsersValueTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dip"
      android:gravity="left"
      android:text="Total Users Value"
      android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
  android:id="@+id/totalCelebritiesTableRow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  <TextView
      android:id="@+id/totalCelebritiesTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="10dip"
      android:gravity="right"
      android:text="Total Celebrities"
      android:textAppearance="?android:attr/textAppearanceMedium" />
  <TextView
      android:id="@+id/totalCelebritiesValueTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dip"
      android:gravity="left"
      android:text="Total Celebrities Value"
      android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
<TableRow
  android:id="@+id/yourRatingsTableRow"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content" >
  <TextView
      android:id="@+id/yourRatingsTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginRight="10dip"
      android:gravity="right"
      android:text="Your Ratings"
      android:textAppearance="?android:attr/textAppearanceMedium" />
  <TextView
      android:id="@+id/yourRatingsValueTextView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dip"
      android:gravity="left"
      android:text="Your Ratings Value"
      android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>

Your scene should look something like this:

Next, make sure and connect up your UI components in the onCreate method of TopCelebActivity:

m_totalRatingsValueTextView     = (TextView)  findViewById(R.id.totalRatingsValueTextView);
m_totalUsersValueTextView       = (TextView)  findViewById(R.id.totalUsersValueTextView);
m_totalCelebritiesValueTextView = (TextView)  findViewById(R.id.totalCelebritiesValueTextView);
m_yourRatingsValueTextView      = (TextView)  findViewById(R.id.yourRatingsValueTextView);

Lastly, we need to call the getStats method by adding the following line to the onResume method of TopCelebActivity:

getStats();

That’s it!

Now, as soon as ratings are being captured in WouldYa’s, then we can call the server extension that will aggregate the statistics on the fly and return a StatsObject that can be used to populate the scene.

Final result: We successfully created a StatsObject on the API from an server extension that updates the corresponding UI components.

NEXT: Push Notifications