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.annotation.Nullable;
     20 import android.database.DataSetObservable;
     21 import android.database.DataSetObserver;
     22 import android.view.View;
     23 import android.view.ViewGroup;
     24 
     25 /**
     26  * Common base class of common implementation for an {@link Adapter} that can be
     27  * used in both {@link ListView} (by implementing the specialized
     28  * {@link ListAdapter} interface) and {@link Spinner} (by implementing the
     29  * specialized {@link SpinnerAdapter} interface).
     30  */
     31 public abstract class BaseAdapter implements ListAdapter, SpinnerAdapter {
     32     private final DataSetObservable mDataSetObservable = new DataSetObservable();
     33     private CharSequence[] mAutofillOptions;
     34 
     35     public boolean hasStableIds() {
     36         return false;
     37     }
     38 
     39     public void registerDataSetObserver(DataSetObserver observer) {
     40         mDataSetObservable.registerObserver(observer);
     41     }
     42 
     43     public void unregisterDataSetObserver(DataSetObserver observer) {
     44         mDataSetObservable.unregisterObserver(observer);
     45     }
     46 
     47     /**
     48      * Notifies the attached observers that the underlying data has been changed
     49      * and any View reflecting the data set should refresh itself.
     50      */
     51     public void notifyDataSetChanged() {
     52         mDataSetObservable.notifyChanged();
     53     }
     54 
     55     /**
     56      * Notifies the attached observers that the underlying data is no longer valid
     57      * or available. Once invoked this adapter is no longer valid and should
     58      * not report further data set changes.
     59      */
     60     public void notifyDataSetInvalidated() {
     61         mDataSetObservable.notifyInvalidated();
     62     }
     63 
     64     public boolean areAllItemsEnabled() {
     65         return true;
     66     }
     67 
     68     public boolean isEnabled(int position) {
     69         return true;
     70     }
     71 
     72     public View getDropDownView(int position, View convertView, ViewGroup parent) {
     73         return getView(position, convertView, parent);
     74     }
     75 
     76     public int getItemViewType(int position) {
     77         return 0;
     78     }
     79 
     80     public int getViewTypeCount() {
     81         return 1;
     82     }
     83 
     84     public boolean isEmpty() {
     85         return getCount() == 0;
     86     }
     87 
     88     @Override
     89     public CharSequence[] getAutofillOptions() {
     90         return mAutofillOptions;
     91     }
     92 
     93     /**
     94      * Sets the value returned by {@link #getAutofillOptions()}
     95      */
     96     public void setAutofillOptions(@Nullable CharSequence... options) {
     97         mAutofillOptions = options;
     98     }
     99 }
    100