1 /* 2 * Copyright (C) 2007 The Android Open Source Project 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 17 package com.android.development; 18 19 import android.content.Context; 20 import android.view.View; 21 import android.view.ViewGroup; 22 import android.view.LayoutInflater; 23 import android.widget.BaseAdapter; 24 25 import java.util.List; 26 27 public abstract class ArrayAdapter<E> extends BaseAdapter 28 { 29 public ArrayAdapter(Context context, int layoutRes) { 30 mContext = context; 31 mInflater = (LayoutInflater)context.getSystemService( 32 Context.LAYOUT_INFLATER_SERVICE); 33 mLayoutRes = layoutRes; 34 } 35 36 public void setSource(List<E> list) { 37 mList = list; 38 } 39 40 public abstract void bindView(View view, E item); 41 42 public E itemForPosition(int position) { 43 if (mList == null) { 44 return null; 45 } 46 47 return mList.get(position); 48 } 49 50 public int getCount() { 51 return mList != null ? mList.size() : 0; 52 } 53 54 public Object getItem(int position) { 55 return position; 56 } 57 58 public long getItemId(int position) { 59 return position; 60 } 61 62 public View getView(int position, View convertView, ViewGroup parent) { 63 View view; 64 if (convertView == null) { 65 view = mInflater.inflate(mLayoutRes, parent, false); 66 } else { 67 view = convertView; 68 } 69 bindView(view, mList.get(position)); 70 return view; 71 } 72 73 private final Context mContext; 74 private final LayoutInflater mInflater; 75 private final int mLayoutRes; 76 private List<E> mList; 77 } 78 79