Skip to content

Instantly share code, notes, and snippets.

@m0nst3rdr1v3n
Created October 30, 2016 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save m0nst3rdr1v3n/71093f0046b8d144134b3a5bc9fcdd55 to your computer and use it in GitHub Desktop.
Save m0nst3rdr1v3n/71093f0046b8d144134b3a5bc9fcdd55 to your computer and use it in GitHub Desktop.
Result Adapter for Tour Guide App
public class ResultsAdapter extends ArrayAdapter<Results> {
/**
* Resource ID for the background color for this list of words
*/
private int mColorResourceId;
/**
* Create a new {@link ResultsAdapter} object.
*/
public ResultsAdapter(Context context, ArrayList<Results> results, int colorResourceId) {
super(context, 0, results);
mColorResourceId = colorResourceId;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null) {
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Result} object located at this position in the list
Results currentResult = getItem(position);
// Find the TextView in the results_list.xml layout with the ID title name.
TextView titleNameTextView = (TextView) listItemView.findViewById(R.id.title_name);
titleNameTextView.setText(currentResult.getNameOfBusiness());
// Find the TextView in the results_list.xml layout with the ID business_address
TextView businessAddressTextView = (TextView) listItemView.findViewById(R.id.business_address);
businessAddressTextView.setText(currentResult.getBusinessAddress());
// Find the TextView in the results_list.xml layout with the ID business_phone_number
TextView businessPhoneNumberTextView = (TextView) listItemView.findViewById(R.id.business_phone_number);
businessPhoneNumberTextView.setText(currentResult.getBusinessPhoneNumber());
// Find the ImageView in the list_item.xml layout with the ID image.
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image);
imageView.setImageResource(currentResult.getmImageResourceId());
// Set the theme color for the list item
View textContainer = listItemView.findViewById(R.id.text_container);
// Find the color that the resource ID maps to
int color = ContextCompat.getColor(getContext(), mColorResourceId);
// Set the background color of the text container View
textContainer.setBackgroundColor(color);
// Return the whole list item layout (containing 3 TextViews and 1 image view) so that it can be shown in
// the ListView.
return listItemView;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment