2626import com .google .gson .JsonElement ;
2727import com .google .gson .JsonParser ;
2828
29+ import java .io .IOException ;
2930import java .io .InputStream ;
3031import java .io .InputStreamReader ;
3132import java .util .ArrayList ;
@@ -38,7 +39,7 @@ public class RealmExampleActivity extends Activity implements AdapterView.OnItem
3839
3940 public static final String TAG = RealmExampleActivity .class .getName ();
4041
41- private GridView mGrid ;
42+ private GridView mGridView ;
4243 private CityAdapter mAdapter ;
4344
4445 @ Override
@@ -51,28 +52,32 @@ protected void onCreate(Bundle savedInstanceState) {
5152 public void onResume () {
5253 super .onResume ();
5354
55+ // Load from file "cities.json" first time
5456 if (mAdapter == null ) {
5557 List <City > cities = loadCities ();
58+
59+ //This is the GridView adapter
5660 mAdapter = new CityAdapter (this );
5761 mAdapter .setData (cities );
5862
59- mGrid = (GridView ) findViewById (R .id .cities_list );
60- mGrid .setAdapter (mAdapter );
61- mGrid .setOnItemClickListener (RealmExampleActivity .this );
63+ //This is the GridView which will display the list of cities
64+ mGridView = (GridView ) findViewById (R .id .cities_list );
65+ mGridView .setAdapter (mAdapter );
66+ mGridView .setOnItemClickListener (RealmExampleActivity .this );
6267 mAdapter .notifyDataSetChanged ();
63- mGrid .invalidate ();
68+ mGridView .invalidate ();
6469 }
6570 }
6671
6772 public List <City > loadCities () {
6873 List <City > items = new ArrayList <City >();
6974
70- //In this case we're loading from local assets.
71- //NOTE: could alternatively easily load from network
75+ // In this case we're loading from local assets.
76+ // NOTE: could alternatively easily load from network
7277 InputStream stream = null ;
7378 try {
7479 stream = getAssets ().open ("cities.json" );
75- } catch (Exception e ) {
80+ } catch (IOException e ) {
7681 return null ;
7782 }
7883
@@ -85,11 +90,15 @@ public List<City> loadCities() {
8590 // Store the retrieved items to the Realm
8691 Realm realm = Realm .getInstance (this );
8792
93+ // Open a transaction to store items into the realm
8894 realm .beginTransaction ();
8995 for (JsonElement e : array ) {
96+ // Create a realm capable object
9097 City realmCity = realm .createObject (City .class );
9198 realmCity .setName (e .getAsJsonObject ().get ("name" ).getAsString ());
9299 realmCity .setVotes (e .getAsJsonObject ().get ("votes" ).getAsInt ());
100+ // Minor optimization to keep the new cities in a list
101+ // so it doesn't have to be reloaded the first time
93102 items .add (realmCity );
94103 }
95104 realm .commitTransaction ();
@@ -99,20 +108,27 @@ public List<City> loadCities() {
99108
100109 public void updateCities () {
101110 Realm realm = Realm .getInstance (this );
111+
112+ // Pull all the cities from the realm
102113 RealmResults <City > cities = realm .where (City .class ).findAll ();
114+
115+ // Put these items in the Adapter
103116 mAdapter .setData (cities );
104117 mAdapter .notifyDataSetChanged ();
105- mGrid .invalidate ();
118+ mGridView .invalidate ();
106119 }
107120
108121 @ Override
109122 public void onItemClick (AdapterView <?> parent , View view , int position , long id ) {
110123 City modifiedCity = (City )mAdapter .getItem (position );
111124
112- //Update the realm object affected by the user
125+ // Update the realm object affected by the user
113126 Realm realm = Realm .getInstance (this );
127+
128+ // Acquire the list of realm cities matching the name of the clicked City.
114129 City city = realm .where (City .class ).equalTo ("name" , modifiedCity .getName ()).findFirst ();
115130
131+ // Create a transaction to increment the vote count for the selected City in the realm
116132 realm .beginTransaction ();
117133 city .setVotes (city .getVotes () + 1 );
118134 realm .commitTransaction ();
0 commit comments