如何按最新的排序方式对我的列表视图进行排序?

时间:2013-12-13 04:33:50

标签: android

我想对listview进行排序,以便添加的最新数据位于顶部。我试图通过使用order by子句和for循环进行排序,如从其他Q&一个 。但我无法得到结果。请帮忙。谢谢。

查看课程

@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();



    }

我的getAllData方法

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;
    }

4 个答案:

答案 0 :(得分:3)

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

或者你可以实现自己的比较器来排序并消除相反的步骤:

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

或者更简单地使用Collections.reverseOrder(),因为你只是在逆转:

Collections.sort(list, Collections.reverseOrder());

答案 1 :(得分:2)

在索引0

添加列表项
 _productlist.add(0,_ProductModel);

它对我有用。试试看。 添加对象后,列表调用notifyDataSetChanged()

listAdapter.notifyDataSetChanged();

以下是一个例子:

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();

            }
        });

    }

答案 2 :(得分:0)

我会使用#dipali建议的内容,但在listviews适配器上调用notifyDataSetChanged()。

答案 3 :(得分:0)

你可以用三种不同的方式做到这一点。

  1. 您可以在数据库表中添加时间戳列。更新此列,即可添加或编辑产品,并根据时间戳对产品进行排序。

  2. 您可以在查询中使用ORDER BY DESC来获取有序结果。

  3. 按顺序从适配器设置值 for(int i = productlist.size(); i&gt; 0; i--) {

    }

相关问题