Skip to content

Commit 8acfaf4

Browse files
committed
changed public data field to private data field
1 parent 02e0afc commit 8acfaf4

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

app/src/main/java/com/codepath/example/customadapterdemo/CustomUsersAdapter.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,21 @@ public CustomUsersAdapter(Context context, ArrayList<User> users) {
1616

1717
@Override
1818
public View getView(int position, View convertView, ViewGroup parent) {
19-
// Get the data item for this position
20-
User user = getItem(position);
19+
2120
// Check if an existing view is being reused, otherwise inflate the view
2221
if (convertView == null) {
2322
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
2423
}
25-
// Lookup view for data population
24+
25+
// Get the data item for this position
26+
User user = getItem(position);
27+
28+
// Lookup view for data population
2629
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
2730
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
2831
// Populate the data into the template view using the data object
29-
tvName.setText(user.name);
30-
tvHome.setText(user.hometown);
32+
tvName.setText(user.getName());
33+
tvHome.setText(user.getHometown());
3134
// Return the completed view to render on screen
3235
return convertView;
3336
}

app/src/main/java/com/codepath/example/customadapterdemo/User.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,26 @@
33
import java.util.ArrayList;
44

55
public class User {
6-
public String name;
7-
public String hometown;
6+
7+
//declare private data instead of public to ensure the privacy of data field of each class
8+
private String name;
9+
private String hometown;
810

911
public User(String name, String hometown) {
1012
this.name = name;
1113
this.hometown = hometown;
1214
}
1315

16+
//retrieve user's name
17+
public String getName(){
18+
return name;
19+
}
20+
21+
//retrieve users' hometown
22+
public String getHometown(){
23+
return hometown;
24+
}
25+
1426
public static ArrayList<User> getUsers() {
1527
ArrayList<User> users = new ArrayList<User>();
1628
users.add(new User("Harry", "San Diego"));

0 commit comments

Comments
 (0)