Search code examples
sqlitesortinglistviewandroid-studiopriority-queue

How to sort sqlite with many conditions in android studio?


I have to sort my listview with priority. First, I need to sort the listview by duedate, if the duedate is same, then I need to sort it by assessment mark. If the assessment mark is same, then I need to sort it by time completion. It's a planner app. May I know how can I sort it? I'm new to android studio. I built the code through youtube tutorials.

listDataAdapter=new ListDataAdapter(getApplicationContext(),R.layout.row_layout);
listview.setAdapter(listDataAdapter);
userDBHelper=new UserDbHelper(getApplicationContext());
sqLiteDatabase=userDBHelper.getReadableDatabase();
cursor=userDBHelper.getInformation(sqLiteDatabase);
    if (cursor.moveToFirst())
            {
                do {
                    String title,duedate,time_submission,time_completion,assessmentmark;
                    title=cursor.getString(0);
                    duedate=cursor.getString(1);
                    time_submission=cursor.getString(2);
                    time_completion=cursor.getString(3);
                    assessmentmark=cursor.getString(4);
                    DataProvider dataProvider=new DataProvider(title,duedate,time_submission,time_completion,assessmentmark);
                    listDataAdapter.add(dataProvider);

                }while (cursor.moveToNext());
            }
public Cursor getInformation(SQLiteDatabase db)
    {
        Cursor cursor;
        String[] projections={UserData.NewTaskInfo.TITLE, UserData.NewTaskInfo.DUEDATE, UserData.NewTaskInfo.TIME_SUBMISSION,
                UserData.NewTaskInfo.TIME_COMPLETION, UserData.NewTaskInfo.ASSESSMENTMARK}; 
        cursor=db.query(UserData.NewTaskInfo.TABLE_NAME,projections,null,null,null,null,null);
        return cursor;

    }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row=convertView;
    layoutHandler layoutHandler;
    if (row==null)
    {
        LayoutInflater layoutInflater=(LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row=layoutInflater.inflate(R.layout.row_layout,parent,false);
        layoutHandler =new layoutHandler();
        layoutHandler.TITLE=(TextView) row.findViewById(R.id.text_title);
        layoutHandler.DUEDATE=(TextView)row.findViewById(R.id.text_duedate);
        layoutHandler.TIMESUBMISSION=(TextView)row.findViewById(R.id.text_time_submission);
        layoutHandler.TIMECOMPLETION=(TextView)row.findViewById(R.id.text_time_completion);
        layoutHandler.ASSESSMENTMARK=(TextView)row.findViewById(R.id.text_assessmentmark);
        row.setTag(layoutHandler);
    }else{        //if the row already existing

        layoutHandler=(layoutHandler) row.getTag();

    }
    DataProvider dataProvider=(DataProvider) this.getItem(position);
    layoutHandler.TITLE.setText(dataProvider.getTitle());
    layoutHandler.DUEDATE.setText(dataProvider.getDuedate());
    layoutHandler.TIMESUBMISSION.setText(dataProvider.getTime_submission());
    layoutHandler.TIMECOMPLETION.setText(dataProvider.getTime_completion());
    layoutHandler.ASSESSMENTMARK.setText(dataProvider.getAssessmentmark());
    return row;
}

Solution

  • The seventh parameter of the query() method is an SQL ORDER BY clause. Such a clause can speficy multiple columns:

    String[] projections={UserData.NewTaskInfo.TITLE, UserData.NewTaskInfo.DUEDATE, UserData.NewTaskInfo.TIME_SUBMISSION,
            UserData.NewTaskInfo.TIME_COMPLETION, UserData.NewTaskInfo.ASSESSMENTMARK};
    String orderBy = UserData.NewTaskInfo.DUEDATE + "," +
                     UserData.NewTaskInfo.ASSESSMENTMARK + "," +
                     UserData.NewTaskInfo.TIME_COMPLETION;
    cursor = db.query(UserData.NewTaskInfo.TABLE_NAME, projections, null,
                      null, null, null, orderBy);