Home | History | Annotate | Download | only in widget
      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 android.widget;
     18 
     19 import android.content.Context;
     20 import android.content.res.Resources;
     21 import android.database.Cursor;
     22 import android.view.ContextThemeWrapper;
     23 import android.view.View;
     24 import android.view.ViewGroup;
     25 import android.view.LayoutInflater;
     26 
     27 
     28 /**
     29  * An easy adapter that creates views defined in an XML file. You can specify
     30  * the XML file that defines the appearance of the views.
     31  */
     32 public abstract class ResourceCursorAdapter extends CursorAdapter {
     33     private int mLayout;
     34 
     35     private int mDropDownLayout;
     36 
     37     private LayoutInflater mInflater;
     38     private LayoutInflater mDropDownInflater;
     39 
     40     /**
     41      * Constructor the enables auto-requery.
     42      *
     43      * @deprecated This option is discouraged, as it results in Cursor queries
     44      * being performed on the application's UI thread and thus can cause poor
     45      * responsiveness or even Application Not Responding errors.  As an alternative,
     46      * use {@link android.app.LoaderManager} with a {@link android.content.CursorLoader}.
     47      *
     48      * @param context The context where the ListView associated with this adapter is running
     49      * @param layout resource identifier of a layout file that defines the views
     50      *            for this list item.  Unless you override them later, this will
     51      *            define both the item views and the drop down views.
     52      */
     53     @Deprecated
     54     public ResourceCursorAdapter(Context context, int layout, Cursor c) {
     55         super(context, c);
     56         mLayout = mDropDownLayout = layout;
     57         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     58         mDropDownInflater = mInflater;
     59     }
     60 
     61     /**
     62      * Constructor with default behavior as per
     63      * {@link CursorAdapter#CursorAdapter(Context, Cursor, boolean)}; it is recommended
     64      * you not use this, but instead {@link #ResourceCursorAdapter(Context, int, Cursor, int)}.
     65      * When using this constructor, {@link #FLAG_REGISTER_CONTENT_OBSERVER}
     66      * will always be set.
     67      *
     68      * @param context The context where the ListView associated with this adapter is running
     69      * @param layout resource identifier of a layout file that defines the views
     70      *            for this list item.  Unless you override them later, this will
     71      *            define both the item views and the drop down views.
     72      * @param c The cursor from which to get the data.
     73      * @param autoRequery If true the adapter will call requery() on the
     74      *                    cursor whenever it changes so the most recent
     75      *                    data is always displayed.  Using true here is discouraged.
     76      */
     77     public ResourceCursorAdapter(Context context, int layout, Cursor c, boolean autoRequery) {
     78         super(context, c, autoRequery);
     79         mLayout = mDropDownLayout = layout;
     80         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     81         mDropDownInflater = mInflater;
     82     }
     83 
     84     /**
     85      * Standard constructor.
     86      *
     87      * @param context The context where the ListView associated with this adapter is running
     88      * @param layout Resource identifier of a layout file that defines the views
     89      *            for this list item.  Unless you override them later, this will
     90      *            define both the item views and the drop down views.
     91      * @param c The cursor from which to get the data.
     92      * @param flags Flags used to determine the behavior of the adapter,
     93      * as per {@link CursorAdapter#CursorAdapter(Context, Cursor, int)}.
     94      */
     95     public ResourceCursorAdapter(Context context, int layout, Cursor c, int flags) {
     96         super(context, c, flags);
     97         mLayout = mDropDownLayout = layout;
     98         mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
     99         mDropDownInflater = mInflater;
    100     }
    101 
    102     /**
    103      * Sets the {@link android.content.res.Resources.Theme} against which drop-down views are
    104      * inflated.
    105      * <p>
    106      * By default, drop-down views are inflated against the theme of the
    107      * {@link Context} passed to the adapter's constructor.
    108      *
    109      * @param theme the theme against which to inflate drop-down views or
    110      *              {@code null} to use the theme from the adapter's context
    111      * @see #newDropDownView(Context, Cursor, ViewGroup)
    112      */
    113     @Override
    114     public void setDropDownViewTheme(Resources.Theme theme) {
    115         super.setDropDownViewTheme(theme);
    116 
    117         if (theme == null) {
    118             mDropDownInflater = null;
    119         } else if (theme == mInflater.getContext().getTheme()) {
    120             mDropDownInflater = mInflater;
    121         } else {
    122             final Context context = new ContextThemeWrapper(mContext, theme);
    123             mDropDownInflater = LayoutInflater.from(context);
    124         }
    125     }
    126 
    127     /**
    128      * Inflates view(s) from the specified XML file.
    129      *
    130      * @see android.widget.CursorAdapter#newView(android.content.Context,
    131      *      android.database.Cursor, ViewGroup)
    132      */
    133     @Override
    134     public View newView(Context context, Cursor cursor, ViewGroup parent) {
    135         return mInflater.inflate(mLayout, parent, false);
    136     }
    137 
    138     @Override
    139     public View newDropDownView(Context context, Cursor cursor, ViewGroup parent) {
    140         return mDropDownInflater.inflate(mDropDownLayout, parent, false);
    141     }
    142 
    143     /**
    144      * <p>Sets the layout resource of the item views.</p>
    145      *
    146      * @param layout the layout resources used to create item views
    147      */
    148     public void setViewResource(int layout) {
    149         mLayout = layout;
    150     }
    151 
    152     /**
    153      * <p>Sets the layout resource of the drop down views.</p>
    154      *
    155      * @param dropDownLayout the layout resources used to create drop down views
    156      */
    157     public void setDropDownViewResource(int dropDownLayout) {
    158         mDropDownLayout = dropDownLayout;
    159     }
    160 }
    161