When retrieving values from the database, you can sort them in ascending / descending order before retrieving.
number | title |
---|---|
15 | ABCD |
67 | EFGH |
22 | IJKL |
12 | MNOP |
For example, suppose there is a value in the database (DB) as above
if(helper == null){
helper = new TestOpenHelper(getActivity().getApplicationContext());
}
if(db == null){
db = helper.getReadableDatabase();
}
Cursor cursor = db.query(
"testdb",
new String[] { "number","title" },
null, //selection
null, //selectionArgs
null, //groupBy
null, //having
null, //orderBy
);
If you just want to get the DB in the order of registration, this method is fine.
if(helper == null){
helper = new TestOpenHelper(getActivity().getApplicationContext());
}
if(db == null){
db = helper.getReadableDatabase();
}
String order_by = "number ASC"; //The value you want to sort,ascending order(ASC)Or descending order(DESC)Or
Cursor cursor = db.query(
"testdb",
new String[] { "number","title" },
null, //selection
null, //selectionArgs
null, //groupBy
null, //having
order_by
);
Specify in order By when assigning a value to Cursor → Just specify ASC for ascending order and DESC for descending order!
//Turn the data list
cursor.moveToFirst();
for (int i = 0; i < cursor.getCount(); i++) {
//Write arbitrary processing here
cursor.moveToNext();
}
cursor.close();
Make a note of how to get the sorted data, which is often used.
Recommended Posts