I want to sort my listview so that the newest data added is on the top. I've tried to sort by using the order by clause and for loop as seen from other Q & A . But I just can't get the result. Please help. Thank you.
View Class
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
_productlist.clear();
db = new DatabaseHelper(getApplicationContext());
db.getWritableDatabase();
ArrayList<ProductModel> product_list = db.getProudcts();
for (int i = 0; i < product_list.size(); i++) {
String tidno = product_list.get(i).getIdno();
System.out.println("tidno>>>>>" + tidno);
String tname = product_list.get(i).getProductname();
String tprice = product_list.get(i).getProductprice();
String tadd = product_list.get(i).getProductadd();
ProductModel _ProductModel = new ProductModel();
_ProductModel.setIdno(tidno);
_ProductModel.setProductname(tname);
_ProductModel.setProductprice(tprice);
_ProductModel.setProductadd(tadd);
_productlist.add(_ProductModel);
}
listview.setAdapter(new ListAdapter(this));
db.close();
}
My getAllData method
public ArrayList<ProductModel> getProudcts() {
cartList.clear();
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("select * from producttable", null);
if (cursor.getCount() != 0) {
if (cursor.moveToFirst()) {
do {
ProductModel item = new ProductModel();
item.idno = cursor.getString(cursor
.getColumnIndex("productidno"));
item.productname = cursor.getString(cursor
.getColumnIndex("productname"));
item.productprice = cursor.getString(cursor
.getColumnIndex("productprice"));
item.productadd = cursor.getString(cursor
.getColumnIndex("productadd"));
cartList.add(item);
} while (cursor.moveToNext());
}
}
cursor.close();
db.close();
return cartList;
}