1 /* 2 * Copyright (C) 2017 Google Inc. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 package com.example.android.wearable.wear.wearaccessibilityapp; 17 18 import android.content.Context; 19 import android.support.annotation.NonNull; 20 import android.support.annotation.Nullable; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.ViewGroup; 24 import android.widget.ArrayAdapter; 25 import android.widget.ImageView; 26 import android.widget.TextView; 27 28 import java.util.List; 29 30 public class ListViewAdapter<T extends Item> extends ArrayAdapter<T> { 31 private final LayoutInflater mInflater; 32 private List<T> mItems; 33 34 public ListViewAdapter(@NonNull Context context, @NonNull List<T> items) { 35 super(context, R.layout.list_item_layout, items); 36 mInflater = LayoutInflater.from(context); 37 mItems = items; 38 } 39 40 @NonNull 41 @Override 42 public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { 43 Holder holder; 44 if (convertView == null) { 45 convertView = mInflater.inflate(R.layout.list_item_layout, parent, false); 46 holder = new Holder(); 47 holder.mTextView = convertView.findViewById(R.id.item_text); 48 holder.mImageView = convertView.findViewById(R.id.item_image); 49 convertView.setTag(holder); // Cache the holder for future use. 50 } else { 51 holder = (Holder) convertView.getTag(); 52 } 53 holder.mTextView.setText(mItems.get(position).getItemId()); 54 return convertView; 55 } 56 57 private static class Holder { 58 TextView mTextView; 59 ImageView mImageView; 60 } 61 } 62