0

我使用 json 从 web 服务获取所有数据,然后在 gridview 中显示。我的主要任务是对 gridview 数据 AZ、ZA 和价格进行排序

//Here is the code which I used but its not show any kind of result.

switch (position)
 {
                        case 0:

                            searchlist = new ArrayList<CakeC>();
                           Collections.sort(searchlist, new Comparator<CakeC>() {
                               @Override
                               public int compare(CakeC lhs, CakeC rhs) {
                                   return -lhs.product_title.compareTo(rhs.product_title);
                               }
                           });
                            adapter.notifyDataSetChanged();
                            Log.e("Sort Data", searchlist.toString());
                            Toast.makeText(getActivity(), "you choose View" + itemValue, Toast.LENGTH_LONG).show();

                            break;
                        case 1:

                            Comparator<CakeC> cmp = new Comparator<CakeC>() {
                                public CakeC oi1;
                                public CakeC oi2;

                                @Override
                                public int compare(CakeC o1, CakeC o2) {
                                   int oi1= Integer.parseInt(Global.cakename.product_price.toString());
                                    this.oi1=o1;
                                    int oi2= Integer.parseInt(Global.cakename.product_price.toString());
                                    this.oi2=o2;

                                    return oi1 - oi2;
                                }
                            };

                            searchlist = new ArrayList<CakeC>();
                            Collections.sort(searchlist, cmp);
                            adapter.notifyDataSetChanged();
                            Log.e("Sort Values",searchlist.toString());

                            break;

                    }
4

1 回答 1

0

调用以下方法:

开关(位置){案例0:

                            Collections.sort(Global.flowerList, StringAscComparator);
                            adapter = new ListAdapter(getActivity(),
                                    Global.flowerList, TypeOfData.Flowers);
                            mGridView.setAdapter(adapter);
                            adapter.notifyDataSetChanged();
                            Toast.makeText(getActivity(), "you choose View" + itemValue, Toast.LENGTH_LONG).show();

                            break;

进而

//升序比较器

public static Comparator<Flowers> StringAscComparator = new Comparator<Flowers>() {

    @Override
    public int compare(Flowers lhs, Flowers rhs) {
        return lhs.product_title.compareToIgnoreCase(rhs.product_title);
    }


};

//Comparator for Descending Order
public static Comparator<Flowers> StringDescComparator = new Comparator<Flowers>() {

    public int compare(Flowers app1, Flowers app2) {



        return app2.product_title.compareToIgnoreCase(app1.product_title);
    }
};
于 2016-07-16T05:10:09.317 回答