Last active
January 6, 2018 14:44
-
-
Save guilhermekrz/504ea775ad3e15932836 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 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, getItemCount()); | |
} | |
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
Thank you for this! It helped me a lot!
I think I found a bug in this code though. My app crashed when I replaced the cursor with
null
in onPause and I wondered what the reason is. I think this is the result of a wrong argument being passed tonotifyItemRangeRemoved
inCursorRecyclerViewAdapter#swapCursor
.Your code replaces the old cursor with the new one (or null in this case) at line 107. Then in line 116 you call notifyItemRangeRemoved passing
getItemCount()
as second parameter. Because the cursor already changed, this will return the new cursor size (or 0 in this case), not the old one. NotifyItemRangeRemoved requires the second parameter to be the old adapter size though. Therefor, the RecyclerView will throw anIndexOutOfBoundsException
. To reproduce this, simply passnull
as cursor inswapCursor
after the adapter has already been created with a valid cursor.This can easily be fixed by replacing the second argument with the old adapter size: