|
package com.example.android.japanautumntrip2016; |
|
|
|
import android.content.Context; |
|
import android.support.v4.content.ContextCompat; |
|
import android.view.LayoutInflater; |
|
import android.view.View; |
|
import android.view.ViewGroup; |
|
import android.widget.ImageView; |
|
import android.widget.TextView; |
|
import android.widget.ArrayAdapter; |
|
|
|
import java.util.ArrayList; |
|
|
|
/** |
|
* Created by novembergave on 21/12/2016. |
|
*/ |
|
|
|
public class WordAdaptor extends ArrayAdapter<Word> { |
|
|
|
/** Resource ID for the background color for this page */ |
|
private int mColorResourceId; |
|
|
|
public WordAdaptor(Context context, ArrayList<Word> words, int colorResourceId) { |
|
super(context, 0, words); |
|
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 Word} object located at this position in the list |
|
Word currentWord = getItem(position); |
|
|
|
// Find the TextView in the list_item.xml layout with the ID location text view. |
|
TextView locationTextView = (TextView) listItemView.findViewById(R.id.location_text_view); |
|
// Get the name from the currentWord object and set this text on |
|
// the location TextView. |
|
locationTextView.setText(currentWord.getLocationId()); |
|
|
|
// Find the TextView in the list_item.xml layout with the ID description text view. |
|
TextView descriptionTextView = (TextView) listItemView.findViewById(R.id.description_text_view); |
|
// Get the description from the currentWord object and set this text on |
|
// the description TextView. |
|
descriptionTextView.setText(currentWord.getDescriptionId()); |
|
|
|
// Find the ImageView in the list_item.xml layout with the ID image. |
|
ImageView imageView = (ImageView) listItemView.findViewById(R.id.image); |
|
// Check if an image is provided for this word or not |
|
if (currentWord.hasImage()) { |
|
// If an image is available, display the provided image based on the resource ID |
|
imageView.setImageResource(currentWord.getImageResourceId()); |
|
// Make sure the view is visible |
|
imageView.setVisibility(View.VISIBLE); |
|
} else { |
|
// Otherwise hide the ImageView (set visibility to GONE) |
|
imageView.setVisibility(View.GONE); |
|
} |
|
|
|
// 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 2 TextViews) so that it can be shown in |
|
// the ListView. |
|
return listItemView; |
|
} |
|
} |