I have a RecyclerView list of CardView items. I save CardView data to a SQLite database. The user can drag CardViews up and down in the list to change the order of the items. When the user exits the app, I'd like to save the current order of the RecyclerView items. Then when the user re-opens the app, I'd like to restore that exact order of the RecyclerView items.
I have tried multiple approaches based on other SO posts with no luck:
--How to save RecyclerView's scroll position using RecyclerView.State?
--RecyclerView store / restore state between activities
--How to save scroll position of RecyclerView in Android?
What I get each time I re-open the app is my default order based on the CardView's original timestamp. It shows the newest CardView item at the top of the list, descending to the last item which is the oldest CardView.
Here is my code:
public class MainActivity extends AppCompatActivity {
private ArrayList<ListItem> allList;
private RecyclerView mRecyclerView;
private SQLiteDB sqLiteDB;
private MyRecylerAdapter adapter;
private static final String KEY_RECYCLER_STATE = "recycler_state";
private Parcelable recyclerViewState;
protected void onCreate(Bundle savedInstanceState) {
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
allList = new ArrayList<>();
allList.clear();
allList = sqLiteDB.getAllDBItems();
adapter = new MyRecylerAdapter(this, allList);
mRecyclerView.setAdapter(adapter);
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable(KEY_RECYCLER_STATE, mRecyclerView.getLayoutManager().onSaveInstanceState());
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
recyclerViewState = savedInstanceState.getParcelable(KEY_RECYCLER_STATE);
}
@Override
protected void onResume() {
super.onResume();
if (mRecyclerView !=null) {
mRecyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
}
}
}
public class SQLiteDB extends SQLiteOpenHelper {
...
public ArrayList<ListItem> getAllDBItems() {
ArrayList<ListItem> modelList = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
String[] columns = {
ItemContract.ItemEntry.A,
ItemContract.ItemEntry.B,
ItemContract.ItemEntry.C,
ItemContract.ItemEntry.D,
ItemContract.ItemEntry.E,
ItemContract.ItemEntry.F,
ItemContract.ItemEntry.G,
ItemContract.ItemEntry.H,
ItemContract.ItemEntry.I,
ItemContract.ItemEntry.J
};
Cursor getCursor = db.query(
TABLE_NAME,
columns,
null,
null,
null,
null,
null
);
try {
if (getCursor.getCount() > 0) {
getCursor.moveToFirst();
while (!getCursor.isAfterLast()) {
do {
ListItem listItem = new ListItem();
listItem.setId(Integer.parseInt(getCursor.getString(getCursor.getColumnIndex(A))));
listItem.setType(getCursor.getString(getCursor.getColumnIndex(B)));
listItem.setTypeColor(Integer.parseInt(getCursor.getString(getCursor.getColumnIndex(C))));
listItem.setTodo(getCursor.getString(getCursor.getColumnIndex(D)));
listItem.setNote1(getCursor.getString(getCursor.getColumnIndex(E)));
listItem.setNote2(getCursor.getString(getCursor.getColumnIndex(F)));
listItem.setDuedate(getCursor.getString(getCursor.getColumnIndex(G)));
listItem.setDuetime(getCursor.getString(getCursor.getColumnIndex(H))); listItem.setTimestamp(Long.parseLong(getCursor.getString(getCursor.getColumnIndex(I))));
listItem.setRandint(Integer.parseInt(getCursor.getString(getCursor.getColumnIndex(J))));
modelList.add(0,listItem);
} while (getCursor.moveToNext());
}
}
} finally {
if (getCursor != null && !getCursor.isClosed()) {
getCursor.close();
}
} if(db.isOpen()) {
db.close();
}
return modelList;
}
public class ListItem {
private int _id;
private int _sortorder;
public void setSortorder(int sortorder) {
_sortorder = sortorder;
}
}