1 /* 2 * Copyright (C) 2010 Google Inc. 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.loaderapp; 18 19 import com.android.loaderapp.R; 20 import com.android.loaderapp.model.ContactsListLoader; 21 22 import android.content.Context; 23 import android.database.Cursor; 24 import android.view.LayoutInflater; 25 import android.view.View; 26 import android.view.ViewGroup; 27 import android.widget.CursorAdapter; 28 import android.widget.TextView; 29 30 public class CursorFactoryListAdapter extends CursorAdapter { 31 ViewFactory mViewFactory; 32 33 public interface ViewFactory { 34 public View newView(Context context, ViewGroup parent); 35 public void bindView(View view, Context context, Cursor cursor); 36 } 37 38 /** 39 * A simple view factory that inflates the views from XML and puts the display 40 * name in @id/name. 41 */ 42 public static class ResourceViewFactory implements ViewFactory { 43 private int mResId; 44 45 public ResourceViewFactory(int resId) { 46 mResId = resId; 47 } 48 49 public View newView(Context context, ViewGroup parent) { 50 LayoutInflater inflater = (LayoutInflater) context.getSystemService( 51 Context.LAYOUT_INFLATER_SERVICE); 52 return inflater.inflate(mResId, parent, false); 53 } 54 55 public void bindView(View view, Context context, Cursor cursor) { 56 TextView name = (TextView) view.findViewById(R.id.name); 57 name.setText(cursor.getString(ContactsListLoader.COLUMN_NAME)); 58 } 59 } 60 61 public CursorFactoryListAdapter(Context context, ViewFactory factory) { 62 super(context, null, /* disable content observers for the cursor */0); 63 mViewFactory = factory; 64 } 65 66 @Override 67 public View newView(Context context, Cursor cursor, ViewGroup parent) { 68 View view = mViewFactory.newView(context, parent); 69 mViewFactory.bindView(view, context, cursor); 70 return view; 71 } 72 73 @Override 74 public void bindView(View view, Context context, Cursor cursor) { 75 mViewFactory.bindView(view, context, cursor); 76 } 77 } 78