1 /* 2 * Copyright (C) 2006 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.annotation.Nullable; 20 import android.database.DataSetObserver; 21 import android.view.View; 22 import android.view.ViewGroup; 23 24 /** 25 * An Adapter object acts as a bridge between an {@link AdapterView} and the 26 * underlying data for that view. The Adapter provides access to the data items. 27 * The Adapter is also responsible for making a {@link android.view.View} for 28 * each item in the data set. 29 * 30 * @see android.widget.ArrayAdapter 31 * @see android.widget.CursorAdapter 32 * @see android.widget.SimpleCursorAdapter 33 */ 34 public interface Adapter { 35 /** 36 * Register an observer that is called when changes happen to the data used by this adapter. 37 * 38 * @param observer the object that gets notified when the data set changes. 39 */ registerDataSetObserver(DataSetObserver observer)40 void registerDataSetObserver(DataSetObserver observer); 41 42 /** 43 * Unregister an observer that has previously been registered with this 44 * adapter via {@link #registerDataSetObserver}. 45 * 46 * @param observer the object to unregister. 47 */ unregisterDataSetObserver(DataSetObserver observer)48 void unregisterDataSetObserver(DataSetObserver observer); 49 50 /** 51 * How many items are in the data set represented by this Adapter. 52 * 53 * @return Count of items. 54 */ getCount()55 int getCount(); 56 57 /** 58 * Get the data item associated with the specified position in the data set. 59 * 60 * @param position Position of the item whose data we want within the adapter's 61 * data set. 62 * @return The data at the specified position. 63 */ getItem(int position)64 Object getItem(int position); 65 66 /** 67 * Get the row id associated with the specified position in the list. 68 * 69 * @param position The position of the item within the adapter's data set whose row id we want. 70 * @return The id of the item at the specified position. 71 */ getItemId(int position)72 long getItemId(int position); 73 74 /** 75 * Indicates whether the item ids are stable across changes to the 76 * underlying data. 77 * 78 * @return True if the same id always refers to the same object. 79 */ hasStableIds()80 boolean hasStableIds(); 81 82 /** 83 * Get a View that displays the data at the specified position in the data set. You can either 84 * create a View manually or inflate it from an XML layout file. When the View is inflated, the 85 * parent View (GridView, ListView...) will apply default layout parameters unless you use 86 * {@link android.view.LayoutInflater#inflate(int, android.view.ViewGroup, boolean)} 87 * to specify a root view and to prevent attachment to the root. 88 * 89 * @param position The position of the item within the adapter's data set of the item whose view 90 * we want. 91 * @param convertView The old view to reuse, if possible. Note: You should check that this view 92 * is non-null and of an appropriate type before using. If it is not possible to convert 93 * this view to display the correct data, this method can create a new view. 94 * Heterogeneous lists can specify their number of view types, so that this View is 95 * always of the right type (see {@link #getViewTypeCount()} and 96 * {@link #getItemViewType(int)}). 97 * @param parent The parent that this view will eventually be attached to 98 * @return A View corresponding to the data at the specified position. 99 */ getView(int position, View convertView, ViewGroup parent)100 View getView(int position, View convertView, ViewGroup parent); 101 102 /** 103 * An item view type that causes the {@link AdapterView} to ignore the item 104 * view. For example, this can be used if the client does not want a 105 * particular view to be given for conversion in 106 * {@link #getView(int, View, ViewGroup)}. 107 * 108 * @see #getItemViewType(int) 109 * @see #getViewTypeCount() 110 */ 111 static final int IGNORE_ITEM_VIEW_TYPE = AdapterView.ITEM_VIEW_TYPE_IGNORE; 112 113 /** 114 * Get the type of View that will be created by {@link #getView} for the specified item. 115 * 116 * @param position The position of the item within the adapter's data set whose view type we 117 * want. 118 * @return An integer representing the type of View. Two views should share the same type if one 119 * can be converted to the other in {@link #getView}. Note: Integers must be in the 120 * range 0 to {@link #getViewTypeCount} - 1. {@link #IGNORE_ITEM_VIEW_TYPE} can 121 * also be returned. 122 * @see #IGNORE_ITEM_VIEW_TYPE 123 */ getItemViewType(int position)124 int getItemViewType(int position); 125 126 /** 127 * <p> 128 * Returns the number of types of Views that will be created by 129 * {@link #getView}. Each type represents a set of views that can be 130 * converted in {@link #getView}. If the adapter always returns the same 131 * type of View for all items, this method should return 1. 132 * </p> 133 * <p> 134 * This method will only be called when the adapter is set on the {@link AdapterView}. 135 * </p> 136 * 137 * @return The number of types of Views that will be created by this adapter 138 */ getViewTypeCount()139 int getViewTypeCount(); 140 141 static final int NO_SELECTION = Integer.MIN_VALUE; 142 143 /** 144 * @return true if this adapter doesn't contain any data. This is used to determine 145 * whether the empty view should be displayed. A typical implementation will return 146 * getCount() == 0 but since getCount() includes the headers and footers, specialized 147 * adapters might want a different behavior. 148 */ isEmpty()149 boolean isEmpty(); 150 151 /** 152 * Gets a string representation of the adapter data that can help 153 * {@link android.service.autofill.AutofillService} autofill the view backed by the adapter. 154 * 155 * <p> 156 * It should only be set (i.e., non-{@code null} if the values do not represent PII 157 * (Personally Identifiable Information - sensitive data such as email addresses, 158 * credit card numbers, passwords, etc...). For 159 * example, it's ok to return a list of month names, but not a list of usernames. A good rule of 160 * thumb is that if the adapter data comes from static resources, such data is not PII - see 161 * {@link android.view.ViewStructure#setDataIsSensitive(boolean)} for more info. 162 * 163 * @return {@code null} by default, unless implementations override it. 164 */ getAutofillOptions()165 default @Nullable CharSequence[] getAutofillOptions() { 166 return null; 167 } 168 } 169