X Tutup
Skip to content

Commit a0c799d

Browse files
author
rlwaldrop
committed
Updated changes based on pull request feedback
1 parent be6b3ad commit a0c799d

File tree

4 files changed

+33
-18
lines changed

4 files changed

+33
-18
lines changed

examples/gridViewExample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ android {
1414
compileSdkVersion 19
1515
buildToolsVersion "20.0.0"
1616
defaultConfig {
17-
applicationId 'examples.realm.io.gridViewExample'
17+
applicationId 'io.realm.examples.realmgridview'
1818
minSdkVersion 15
1919
targetSdkVersion 19
2020
versionCode 1

examples/gridViewExample/src/main/java/io/realm/examples/realmgridview/City.java

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,4 @@ public void setVotes(long votes) {
3939
this.votes = votes;
4040
}
4141

42-
@Override
43-
public String toString() {
44-
return "City{" +
45-
"name='" + name + '\'' +
46-
", votes=" + votes +
47-
'}';
48-
}
4942
}

examples/gridViewExample/src/main/java/io/realm/examples/realmgridview/CityAdapter.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525

2626
import java.util.List;
2727

28+
// This adapter is strictly to interface with the GridView and doesn't
29+
// particular show much interesting Realm functionality.
30+
31+
// Alternatively from this example,
32+
// a developer could update the getView() to pull items from the Realm.
33+
2834
public class CityAdapter extends BaseAdapter {
2935

3036
public static final String TAG = RealmExampleActivity.class.getName();

examples/gridViewExample/src/main/java/io/realm/examples/realmgridview/RealmExampleActivity.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.google.gson.JsonElement;
2727
import com.google.gson.JsonParser;
2828

29+
import java.io.IOException;
2930
import java.io.InputStream;
3031
import java.io.InputStreamReader;
3132
import 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

Comments
 (0)
X Tutup