Java tutorial
/* * Copyright (C) 2016 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.example.android.saddacampus; 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.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView; import java.util.ArrayList; public class WordAdapter extends ArrayAdapter<Word> { private int mColorResourceId; public WordAdapter(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 miwok_text_view. TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view); // Get the Miwok translation from the currentWord object and set this text on // the Miwok TextView. miwokTextView.setText(currentWord.getMiwokTranslation()); // Find the TextView in the list_item.xml layout with the ID default_text_view. TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view); // Get the default translation from the currentWord object and set this text on // the default TextView. defaultTextView.setText(currentWord.getDefaultTranslation()); // 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; } }