Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,21 @@ public CustomUsersAdapter(Context context, ArrayList<User> users) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
User user = getItem(position);

// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_user, parent, false);
}
// Lookup view for data population

// Get the data item for this position
User user = getItem(position);

// Lookup view for data population
TextView tvName = (TextView) convertView.findViewById(R.id.tvName);
TextView tvHome = (TextView) convertView.findViewById(R.id.tvHometown);
// Populate the data into the template view using the data object
tvName.setText(user.name);
tvHome.setText(user.hometown);
tvName.setText(user.getName());
tvHome.setText(user.getHometown());
// Return the completed view to render on screen
return convertView;
}
Expand Down
16 changes: 14 additions & 2 deletions app/src/main/java/com/codepath/example/customadapterdemo/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,26 @@
import java.util.ArrayList;

public class User {
public String name;
public String hometown;

//declare private data instead of public to ensure the privacy of data field of each class
private String name;
private String hometown;

public User(String name, String hometown) {
this.name = name;
this.hometown = hometown;
}

//retrieve user's name
public String getName(){
return name;
}

//retrieve users' hometown
public String getHometown(){
return hometown;
}

public static ArrayList<User> getUsers() {
ArrayList<User> users = new ArrayList<User>();
users.add(new User("Harry", "San Diego"));
Expand Down