I am making an android based note taking application with categories. I am supposed to sort the categories according to title and date created. I have inserted the date created column in category table in the sqlite database. i have also used the query by using ORDER BY clause and whenever I click on the Title and Date Created option from options menu. The adapter is sorted accordingly. But whenever I restart the application the sorted adapter resets and all the items are appeared unsorted, as the adapter is set in onCreate method. I want to keep the state of sorted adapter, but unable to understand the logic.
I have tried all the possible solutions that comes in my mind, I have used a global variable chkSort in it to determine which query is to be implemented. But whenever I restart the app this variable is initialized to "default and again the unsorted adapter is set.
This is my snippet of two classes:
public class MainScreen extends AppCompatActivity
{
ArrayList<DataModelCategory> lists = new ArrayList<>();
String query,chkSort="default";
RecyclerView recyclerView;
GridLayoutManager gridLayoutManager;
RecyclerView.Adapter adapter;
RecyclerAdapter recyclerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
recyclerView = findViewById(R.id.recyclerMain);
recyclerView.setHasFixedSize(true);
gridLayoutManager = new GridLayoutManager(this, 4);
recyclerView.setLayoutManager(gridLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
adapter = new RecyclerAdapter(this, lists);
recyclerAdapter=new RecyclerAdapter(this,lists);
lists.clear();
final DataAdapterCategory db = new DataAdapterCategory(getApplicationContext());
db.openDB();
Cursor cursor = db.getAllData(chkSort);
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String categName = cursor.getString(1);
DataModelCategory dataModelCategory = new DataModelCategory(id, categName);
lists.add(dataModelCategory);
}
if (!(lists.size() < 1)) {
recyclerView.setAdapter(adapter);
txt.setVisibility(View.INVISIBLE);
}
else {
txt.setVisibility(View.VISIBLE);
}
db.closeDB();
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case R.id.action_notebook: {
// create an alert builder
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(this);
alertBuilder.setTitle("New notebook");
alertBuilder.setCancelable(false);
// set the custom layout
final View customLayout = getLayoutInflater().inflate(R.layout.notebook_dialog, null);
alertBuilder.setView(customLayout);
// add a button
alertBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
EditText editText = customLayout.findViewById(R.id.editName);
String label = editText.getText().toString();
if (label.trim().length() > 0) {
// send data from the AlertDialog to the Activity
sendDialogDataToActivity(editText.getText().toString());
save(editText.getText().toString());
lists.clear();
DataAdapterCategory db = new DataAdapterCategory(getApplicationContext());
db.openDB();
Cursor cursor = db.getAllData(chkSort);
while (cursor.moveToNext()) {
int id = cursor.getInt(0);
String categName = cursor.getString(1);
DataModelCategory dataModelCategory = new DataModelCategory(id, categName);
lists.add(dataModelCategory);
}
if (!(lists.size() < 1)) {
recyclerView.setAdapter(adapter);
//startActivity(new Intent(MainActivity.this,MainActivity2.class));
txt.setVisibility(View.INVISIBLE);
}
db.closeDB();
// database handler
DatabaseHandler dbs = new DatabaseHandler(getApplicationContext());
// inserting new label into database
dbs.insertLabel(label);
// Hiding the keyboard
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(editText.getWindowToken(), 0);
// loading spinner with newly added data
// loadSpinnerData();
}
else {
Toast.makeText(getApplicationContext(), "Please enter label name.",
Toast.LENGTH_SHORT).show();
dialog.cancel();
}
}
});
// negative button
alertBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// create and show the alert dialog
AlertDialog alert = alertBuilder.create();
alert.show();
}
break;
}
}
}
public class DataAdapterCategory extends AppCompatActivity
{
Context c;
SQLiteDatabase db;
DBHelperText helper;
public DataAdapterCategory(Context c) {
this.c = c;
helper=new DBHelperText(c);
}
//INSERT
public long add(String categoryName,String date)
{
try
{
ContentValues cv=new ContentValues();
cv.put(Constants.CATEGORY_NAME,categoryName);
cv.put(Constants.CATEGORY_DATE,date);
return db.insert(Constants.TB_NAME_CAT,Constants.ROW_ID_CAT,cv);
}
catch (SQLException e)
{
e.printStackTrace();
}
return 0;
}
//RETRIEVE OR VIEW
public Cursor getAllData(String chkSort)
{
String[] columns={Constants.ROW_ID_CAT,Constants.CATEGORY_NAME,Constants.CATEGORY_DATE};
if (chkSort.equals("Title"))
{
return db.query(Constants.TB_NAME_CAT,columns,null,null,null,null,Constants.CATEGORY_NAME + " ASC");
}
else if (chkSort.equals("DateCreated"))
{
return db.query(Constants.TB_NAME_CAT,columns,null,null,null,null,Constants.CATEGORY_DATE + " DESC");
}
else
{
return db.query(Constants.TB_NAME_CAT,columns,null,null,null,null,null);
}
}
}
I want to keep the sorted adapter even after restarting the app. For example if the user has clicked on the sort by options menu and clicked to sort the categories according to date created from alert dialog then after restarting the app the adapter should appear sorted according to date created. But unfortunately I am unable to understand that how should it be done because everytime I start app it becomes unsorted. Where should I set the adapter in this case.