ResourceCursorAdapter.java revision b721b47811c065d41b1aec23035e8b46c245c86e
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 17package android.widget; 18 19import android.content.Context; 20import android.database.Cursor; 21import android.view.View; 22import android.view.ViewGroup; 23import android.view.LayoutInflater; 24 25 26/** 27 * An easy adapter that creates views defined in an XML file. You can specify 28 * the XML file that defines the appearance of the views. 29 */ 30public abstract class ResourceCursorAdapter extends CursorAdapter { 31 private int mLayout; 32 33 private int mDropDownLayout; 34 35 private LayoutInflater mInflater; 36 37 /** 38 * Constructor. 39 * 40 * @param context The context where the ListView associated with this adapter is running 41 * @param layout resource identifier of a layout file that defines the views 42 * for this list item. Unless you override them later, this will 43 * define both the item views and the drop down views. 44 */ 45 public ResourceCursorAdapter(Context context, int layout, Cursor c) { 46 super(context, c); 47 mLayout = mDropDownLayout = layout; 48 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 49 } 50 51 /** 52 * Constructor. 53 * 54 * @param context The context where the ListView associated with this adapter is running 55 * @param layout resource identifier of a layout file that defines the views 56 * for this list item. Unless you override them later, this will 57 * define both the item views and the drop down views. 58 * @param c The cursor from which to get the data. 59 * @param autoRequery If true the adapter will call requery() on the 60 * cursor whenever it changes so the most recent 61 * data is always displayed. 62 */ 63 public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) { 64 super(context, c, autoRequery); 65 mLayout = mDropDownLayout = layout; 66 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 67 } 68 69 /** 70 * Constructor. 71 * 72 * @param context The context where the ListView associated with this adapter is running 73 * @param layout resource identifier of a layout file that defines the views 74 * for this list item. Unless you override them later, this will 75 * define both the item views and the drop down views. 76 * @param c The cursor from which to get the data. 77 * @param flags flags used to determine the behavior of the adapter 78 */ 79 public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) { 80 super(context, c, flags); 81 mLayout = mDropDownLayout = layout; 82 mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 83 } 84 85 /** 86 * Inflates view(s) from the specified XML file. 87 * 88 * @see android.widget.CursorAdapter#newView(android.content.Context, 89 * android.database.Cursor, ViewGroup) 90 */ 91 @Override 92 public View newView(Context context, Cursor cursor, ViewGroup parent) { 93 return mInflater.inflate(mLayout, parent, false); 94 } 95 96 @Override 97 public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) { 98 return mInflater.inflate(mDropDownLayout, parent, false); 99 } 100 101 /** 102 * <p>Sets the layout resource of the item views.</p> 103 * 104 * @param layout the layout resources used to create item views 105 */ 106 public void setViewResource(int layout) { 107 mLayout = layout; 108 } 109 110 /** 111 * <p>Sets the layout resource of the drop down views.</p> 112 * 113 * @param dropDownLayout the layout resources used to create drop down views 114 */ 115 public void setDropDownViewResource(int dropDownLayout) { 116 mDropDownLayout = dropDownLayout; 117 } 118} 119