1

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;
    }
Et Andrea
  • 273
  • 4
  • 21
  • Because you are getting data from a Database, you may not be able to guarantee it's order. In which case I would recommend you add some sort or time stamp or index/id to your rows which you can then sort by. – TheIT Dec 13 '13 at 04:39
  • Hope You just need to reverse the list. – keshav Dec 13 '13 at 04:43
  • 1
    where you had order order by desc? – PankajAndroid Dec 13 '13 at 04:45
  • @Pankaj Hi. I've tried to in "Cursor cursor = db.rawQuery("select * from producttable order by desc", null);". But it did not work so I removed it. – Et Andrea Dec 13 '13 at 04:49
  • SEE this http://stackoverflow.com/questions/12974150/android-sqlite-how-to-order-by-time – keshav Dec 13 '13 at 04:55

4 Answers4

3
Collections.sort(list);
Collections.reverse(list);

Or you could implement your own Comparator to sort on and eliminate the reverse step:

Collections.sort(list, new Comparator<Long>() {
    public int compare(Long o1, Long o2) {
        return o2.compareTo(o1);
    }
})

;

Or even more simply use Collections.reverseOrder() since you're only reversing:

Collections.sort(list, Collections.reverseOrder());
dipali
  • 10,966
  • 5
  • 25
  • 51
2

Add yout list items at index 0

 _productlist.add(0,_ProductModel);

It worked for me. Give it a try. and after adding object ot list call notifyDataSetChanged()

listAdapter.notifyDataSetChanged();

Here is an example:

public class MainActivity extends Activity {

    private ListView lv;
    private Button bt;
    int i = 0;
    private ArrayList<Integer> al;
    private ArrayAdapter<Integer> aa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.listView1);
        bt = (Button) findViewById(R.id.button1);

        al = new ArrayList<Integer>();
        aa = new ArrayAdapter<Integer>(getApplicationContext(),
                android.R.layout.simple_list_item_1, al);
        lv.setAdapter(aa);
        bt.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                al.add(0, i++);

                aa.notifyDataSetChanged();

            }
        });

    }
Amitabh Sarkar
  • 1,281
  • 1
  • 13
  • 26
0

I'd go with what #dipali suggested but then call notifyDataSetChanged() on the listviews adapter.

eimmer
  • 1,537
  • 1
  • 10
  • 29
0

You can do it in three different ways.

  1. You can add time stamp column in your database table. Update this column whan you add or edit a product and sort your product according to the time stamp.

  2. You can use ORDER BY DESC in your query to get ordered result.

  3. Set value from Adapter in reverse order for (int i =productlist.size(); i >0 ; i--) {

    }

Vaibhav Agarwal
  • 4,499
  • 3
  • 19
  • 20