如何通过将项目添加到微调器中来对项目进行排序?

时间:2019-02-11 00:06:18

标签: android sql database sorting android-spinner

如何通过将项目添加到微调器中来对项目进行排序?我使用Sql数据库在微调器中保存书签。例如,最后添加的书签应该在上方,并且每当我打开微调器或显示微调器时,必须首先选择微调器项目。

a

1 个答案:

答案 0 :(得分:0)

选择列列表 FROM table_name [条件] [ORDER BY column1,column2,.. columnN] [ASC | DESC];

public List<DataModel> getAllLabels()
{
        List<DataModel > labels = new ArrayList<DataModel>();

        // Select All Query
        String selectQuery = "SELECT column-list from TABLE_NAME ORDER BY column_1 ASC ";

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if(cursor.moveToFirst()) {
            do {
                labels.add(new DataModel(cursor.getString(0),cursor.getString(1)));
            } while (cursor.moveToNext());
        }

        // closing connection
        cursor.close();
        db.close();

        // returning labels
        return labels;
    }

    private void loadSpinnerDataHama() {
        // database handler
        DatabaseSpinner db = new DatabaseSpinner(getApplicationContext());

        // Spinner Drop down elements
        List<DataModel> lables = db.getAllLabels();

        // Creating adapter for spinner
        ArrayAdapter<DataModel> dataAdapter = new ArrayAdapter<DataModel>(this,
                android.R.layout.simple_spinner_item, lables);

        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        // attaching data adapter to spinner
        spinner.setAdapter(dataAdapter);
    }