我想按 android 片段 class 中的 A-Z 、 Z-A 、价格副对自定义 gridview 进行排序
I want to sort custom gridview by A-Z , Z-A , Price vice in android fragment class
我使用 json 从网络服务获取所有数据,然后显示在 gridview 中。
我的主要任务是根据 A-Z、Z-A 和价格对 gridview 数据进行排序
//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;
}
调用以下方法:
开关(位置){
案例 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);
}
};
我使用 json 从网络服务获取所有数据,然后显示在 gridview 中。 我的主要任务是根据 A-Z、Z-A 和价格对 gridview 数据进行排序
//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;
}
调用以下方法:
开关(位置){ 案例 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);
}
};