/*
 * Copyright 2014 Realm Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package io.realm;

import android.content.Context;
import android.view.LayoutInflater;
import android.widget.BaseAdapter;

public abstract class RealmBaseAdapter<T extends RealmObject> extends BaseAdapter {

    protected LayoutInflater inflater;
    protected RealmResults<T> realmResults;
    protected Context context;

    public RealmBaseAdapter(Context context, RealmResults<T> realmResults, boolean automaticUpdate) {
        if (context == null) {
            throw new IllegalArgumentException("Context cannot be null");
        }
        if (realmResults == null) {
            throw new IllegalArgumentException("RealmResults cannot be null");
        }

        this.context = context;
        this.realmResults = realmResults;
        this.inflater = LayoutInflater.from(context);
        if (automaticUpdate) {
            realmResults.getRealm().addChangeListener(new RealmChangeListener() {
                @Override
                public void onChange() {
                    notifyDataSetChanged();
                }
            });
        }
    }

    @Override
    public int getCount() {
        return realmResults.size();
    }

    @Override
    public T getItem(int i) {
        return realmResults.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i; // TODO: find better solution once we have unique IDs
    }

    /**
     * Update the RealmResults associated to the Adapter. Useful when the query has been changed.
     * If the query does not change you might consider using the automaticUpdate feature
     * @param realmResults the new RealmResults coming from the new query.
     */
    public void updateRealmResults(RealmResults<T> realmResults) {
        this.realmResults = realmResults;
        notifyDataSetChanged();
    }
}

@@ -707,16 +707,473 @@
 apter;%0A%0A
+/**%0A * The RealmBaseAdapter class is an abstract utility class for binding UI elements to Realm data,%0A * much like an %7B@link android.widget.CursorAdapter%7D.%0A *%0A * This adapter will automatically handle any updates to its data and call%0A * %7B@link #notifyDataSetChanged()%7D as appropriate.%0A *%0A * The RealmAdapter will no longer get any updates and will throw an error if it is accessed after%0A * the Realm instance from where the RealmResults came is closed.%0A */%0A
 public a
@@ -2274,32 +2274,372 @@
 .get(i);%0A    %7D%0A%0A
+    /**%0A     * Returns the current ID for a item. Note that item ID's are not stable so you cannot rely on%0A     * the item ID being the same after %7B@link #notifyDataSetChanged()%7D or%0A     * %7B@link #updateRealmResults(RealmResults)%7D has been called.%0A     *%0A     * @param i Index of item in the adapter%0A     * @return Current item ID.%0A     */%0A
     @Override%0A  
@@ -2682,18 +2682,8 @@
     
- return i;
  // 
@@ -2733,16 +2733,34 @@
 que IDs%0A
+        return i;%0A
     %7D%0A%0A 
@@ -2953,16 +2953,23 @@
 feature%0A
+     *%0A
      * @

Update RealmBaseAdapter JavaDoc.