@Override public View getView(int i, View view, ViewGroup viewGroup) { if (view == null) { view = mActivity.getLayoutInflater().inflate(mLayout, viewGroup, false); } T model = mSnapshots.getItem(i).getValue(mModelClass); // Call out to subclass to marshall this model into the provided view populateView(view, model); return view; }
/** * @param activity The activity containing the ListView * @param modelClass Firebase will marshall the data at a location into an instance of a class * that you provide * @param modelLayout This is the layout used to represent a single list item. You will be * responsible for populating an instance of the corresponding view with the data from an * instance of modelClass. * @param ref The Firebase location to watch for data changes. Can also be a slice of a location, * using some combination of <code>limit()</code>, <code>startAt()</code>, and <code>endAt() * </code>, */ public FirebaseListAdapter(Activity activity, Class<T> modelClass, int modelLayout, Query ref) { mModelClass = modelClass; mLayout = modelLayout; mActivity = activity; mSnapshots = new FirebaseArray(ref); mSnapshots.setOnChangedListener( new FirebaseArray.OnChangedListener() { @Override public void onChanged(EventType type, int index, int oldIndex) { notifyDataSetChanged(); } }); }
@Override public long getItemId(int i) { // http://stackoverflow.com/questions/5100071/whats-the-purpose-of-item-ids-in-android-listview-adapter return mSnapshots.getItem(i).getKey().hashCode(); }
public Firebase getRef(int position) { return mSnapshots.getItem(position).getRef(); }
@Override public T getItem(int i) { return mSnapshots.getItem(i).getValue(mModelClass); }
@Override public int getCount() { return mSnapshots.getCount(); }
public void cleanup() { // We're being destroyed, let go of our mListener and forget about all of the mModels mSnapshots.cleanup(); }