-
-
Save dnlbauer/53c8925344f127509d23 to your computer and use it in GitHub Desktop.
Ormlite+RecyclerView
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright (C) 2014 [email protected] | |
* | |
* 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. | |
* | |
*/ | |
import android.content.Context; | |
import android.database.Cursor; | |
import android.database.DataSetObserver; | |
import android.support.v7.widget.RecyclerView; | |
// Code from https://gist.github.com/skyfishjy/443b7448f59be978bc59 | |
// Fix to remove observers (when using CursorLoaders) by: https://gist.github.com/quanturium/46541c81aae2a916e31d | |
public abstract class CursorRecyclerViewAdapter<VH extends RecyclerView.ViewHolder> extends RecyclerView.Adapter<VH> { | |
private Cursor mCursor; | |
private boolean mDataValid; | |
private int mRowIdColumn; | |
public CursorRecyclerViewAdapter(Cursor cursor) { | |
mCursor = cursor; | |
mDataValid = cursor != null; | |
mRowIdColumn = mDataValid ? mCursor.getColumnIndexOrThrow("_id") : -1; | |
setHasStableIds(true); | |
} | |
public Cursor getCursor() { | |
return mCursor; | |
} | |
@Override | |
public int getItemCount() { | |
if (mDataValid && mCursor != null) { | |
return mCursor.getCount(); | |
} else { | |
return 0; | |
} | |
} | |
@Override | |
public long getItemId(int position) { | |
if (hasStableIds() && mDataValid && mCursor != null) { | |
if (mCursor.moveToPosition(position)) { | |
return mCursor.getLong(mRowIdColumn); | |
} else { | |
return RecyclerView.NO_ID; | |
} | |
} else { | |
return RecyclerView.NO_ID; | |
} | |
} | |
public Object getItem(int position) { | |
if (mDataValid && mCursor != null) { | |
mCursor.moveToPosition(position); | |
return mCursor; | |
} else { | |
return null; | |
} | |
} | |
public abstract void onBindViewHolder(VH viewHolder, Cursor cursor); | |
@Override | |
public void onBindViewHolder(VH viewHolder, int position) { | |
if (!mDataValid) { | |
throw new IllegalStateException("this should only be called when the cursor is valid"); | |
} | |
if (!mCursor.moveToPosition(position)) { | |
throw new IllegalStateException("couldn't move cursor to position " + position); | |
} | |
onBindViewHolder(viewHolder, mCursor); | |
} | |
/** | |
* Change the underlying cursor to a new cursor. If there is an existing cursor it will be | |
* closed. | |
*/ | |
public void changeCursor(Cursor cursor) { | |
Cursor old = swapCursor(cursor); | |
if (old != null) { | |
old.close(); | |
} | |
} | |
/** | |
* Swap in a new Cursor, returning the old Cursor. Unlike | |
* {@link #changeCursor(Cursor)}, the returned old Cursor is <em>not</em> | |
* closed. | |
*/ | |
public Cursor swapCursor(Cursor newCursor) { | |
if (newCursor == mCursor) { | |
return null; | |
} | |
final int oldCursorSize = getItemCount(); | |
final Cursor oldCursor = mCursor; | |
mCursor = newCursor; | |
if (mCursor != null) { | |
mRowIdColumn = newCursor.getColumnIndexOrThrow("_id"); | |
mDataValid = true; | |
notifyDataSetChanged(); | |
} else { | |
mRowIdColumn = -1; | |
mDataValid = false; | |
// notify the observers about the lack of a data set | |
notifyItemRangeRemoved(0, oldCursorSize); | |
} | |
return oldCursor; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.graphics.Typeface; | |
import android.support.v7.widget.RecyclerView; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
public class DrawerRecyclerViewAdapter extends OrmliteCursorRecyclerViewAdapter<Championship, DrawerRecyclerViewAdapter.ViewHolder> { | |
public DrawerRecyclerViewAdapter(Context context) { | |
super(context); | |
} | |
@Override | |
public DrawerRecyclerViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | |
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.drawer_row, parent, false); | |
return new ViewHolder(v); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder holder, Championship championship) { | |
holder.drawerTextView.setText(championship.getCommonName()); | |
} | |
public static class ViewHolder extends RecyclerView.ViewHolder { | |
public final TextView drawerTextView; | |
public ViewHolder(View drawerView) { | |
super(drawerView); | |
drawerTextView = (TextView) drawerView.findViewById(R.id.drawerTextView); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.database.Cursor; | |
import android.support.v7.widget.RecyclerView; | |
import com.j256.ormlite.android.AndroidDatabaseResults; | |
import com.j256.ormlite.stmt.PreparedQuery; | |
import java.sql.SQLException; | |
// Based on https://github.com/j256/ormlite-android/pull/8 | |
public abstract class OrmliteCursorRecyclerViewAdapter<T, VH extends RecyclerView.ViewHolder> extends CursorRecyclerViewAdapter<VH> { | |
protected PreparedQuery<T> preparedQuery; | |
public OrmliteCursorRecyclerViewAdapter(Context context){ | |
super(context,null); | |
} | |
public abstract void onBindViewHolder(VH holder, T t); | |
public final void onBindViewHolder(VH viewHolder, Cursor cursor){ | |
try { | |
onBindViewHolder(viewHolder, this.cursorToObject(cursor)); | |
} catch (SQLException e) { | |
e.printStackTrace(); | |
} | |
} | |
public final void changeCursor(Cursor cursor) { | |
throw new UnsupportedOperationException("Please use OrmLiteCursorAdapter.changeCursor(Cursor,PreparedQuery) instead"); | |
} | |
public void changeCursor(Cursor cursor, PreparedQuery<T> preparedQuery) { | |
this.setPreparedQuery(preparedQuery); | |
super.changeCursor(cursor); | |
} | |
public void setPreparedQuery(PreparedQuery<T> preparedQuery) { | |
this.preparedQuery = preparedQuery; | |
} | |
public T getTypedItem(int position) { | |
try { | |
return this.cursorToObject((Cursor)getItem(position)); | |
} catch (SQLException var3) { | |
throw new RuntimeException(var3); | |
} | |
} | |
protected T cursorToObject(Cursor cursor) throws SQLException { | |
return this.preparedQuery.mapRow(new AndroidDatabaseResults(cursor, null)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment