Created
October 30, 2016 01:55
-
-
Save m0nst3rdr1v3n/71093f0046b8d144134b3a5bc9fcdd55 to your computer and use it in GitHub Desktop.
Result Adapter for Tour Guide App
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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