如何在android studio中对许多条件进行sqlite排序?

时间:2017-03-13 11:07:12

标签: sqlite sorting listview android-studio priority-queue

我必须优先排序我的列表视图。首先,我需要通过duedate对listview进行排序,如果duedate相同,那么我需要通过评估标记对其进行排序。如果评估标记相同,那么我需要按时间完成排序。这是一个计划应用程序。我可以知道如何分类吗?我是android studio的新手。我通过youtube教程构建了代码。

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;
}

1 个答案:

答案 0 :(得分:0)

query() method的第七个参数是SQL ORDER BY子句。 这样的条款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);