-1

i have a datebase and i want show the records in sqlite from the last to the first record. i tried movetoLast and movetoPrevious in cursor. i want to know how can i do this? I think the solution will be found in these. 1-Main_Activity => Cursor(I think I did not use the movetoLast & movetoPrevious properly in the listView 2-DatebaseHelper class => select * from ... 3-in the Custom Listview

MainActivity Codes:

void showAllRegisters() {
    //adp_detail_list = null;
    mClDetailShow = new ArrayList<>();
    Database_Cursor_Object();
    while (cursor.moveToNext()) {
        mClDetailShow.add(new Cl_Detail_Items(cursor.getString(4), cursor.getString(3),
                cursor.getString(2), cursor.getString(1), cursor.getString(0),cursor.getString(5)));
    }
    adp_detail_list = new ADP_Detail_List(getActivity().getApplicationContext(), mClDetailShow);
    listView.setAdapter(adp_detail_list);

}

Datebase Helper whole codes

 package ir.lilola.org;

 import android.content.ContentValues;
 import android.content.Context;
 import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 import android.database.sqlite.SQLiteOpenHelper;

 public class Cl_DbHelper extends SQLiteOpenHelper {

private static final String Database_name="stu.db";
private static final String Tbl_name="Tbl_stu";
private static final String ColumnData ="Data";
private static final String ColumnTime ="Time";
private static final String ColumnPrice ="Price";
private static final String ColumnLabel ="Label";
private static final String ColumnCategory ="Category";
private static final String ColumnId ="ID";


private SQLiteDatabase sql = this.getWritableDatabase();
Cl_DbHelper(Context context) {
    super(context, Database_name, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table " + Tbl_name + " (Data TEXT,Time TEXT,Price TEXT,Label TEXT,Category TEXT , ID INTEGER PRIMARY KEY AUTOINCREMENT) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + Tbl_name );
    onCreate(db);
}
public boolean delete_information(String Data,String Time,String Price,String Label,String Category){
    sql.delete(Tbl_name,"Data=? AND Time=? AND Price=? AND Label=? AND Category=?",
            new String[]{Data,Time,Price,Label,Category});
            return true;
}
public boolean update_information(String Data,String Time,String Price,String Label,String Category,String Id){
    ContentValues contentValues = new ContentValues();
    contentValues.put(ColumnData,Data);
    contentValues.put(ColumnTime,Time);
    contentValues.put(ColumnPrice,Price);
    contentValues.put(ColumnLabel,Label);
    contentValues.put(ColumnCategory,Category);
    contentValues.put(ColumnId,Id);
    long check=sql.update(Tbl_name,contentValues,"ID=?",
            new String[]{Id});
    return check != -1;
}
public Cursor select_information(){
    Cursor Resultcursor=sql.rawQuery("select * from " +Tbl_name,null);
    return Resultcursor;
}
public boolean insert_information(String Data,String Time,String Price,String Label,String Category){
    ContentValues contentValues = new ContentValues();
    contentValues.put(ColumnData,Data);
    contentValues.put(ColumnTime,Time);
    contentValues.put(ColumnPrice,Price);
    contentValues.put(ColumnLabel,Label);
    contentValues.put(ColumnCategory,Category);
    long check=sql.insert(Tbl_name,null,contentValues);
    return check != -1;
}
}
Mehran
  • 53
  • 9

2 Answers2

1
SELECT * FROM `Tbl_stu` ORDER by ID DESC

public Cursor select_information(){
    Cursor Resultcursor=sql.rawQuery("SELECT * FROM `Tbl_stu` ORDER by ID DESC,null);
    return Resultcursor;
}
Harshal Deshmukh
  • 1,787
  • 3
  • 14
  • 25
0

Change to:

Cursor Resultcursor = sql.rawQuery("select * from " + Tbl_name + " ORDER BY " + ColumnData + " DESC", null);

if you want the sort by ColumnData.

m00am
  • 5,910
  • 11
  • 53
  • 69