I want to sort items of the ListView by product name. I have a vector called "data" which is a type of class.
The class I have is:
public static class RowData implements Comparable<RowData>{
public String mProductName;
protected int mId;
protected int mOnHand;
protected double mPrice;
protected boolean mIsColleaction;
protected boolean mIsPrePack;
RowData(int id ,String productName,int onhand,double price,boolean IsColleaction, boolean IsPrePack) {
mId= id;
mProductName= productName;
mOnHand =onhand;
mPrice = price;
mIsColleaction =IsColleaction;
mIsPrePack = IsPrePack;
}
@Override
public String toString() {
return mProductName;
}
public int compareTo(RowData other) {
return mProductName.compareTo(other.mProductName);
}
public static Comparator<RowData> COMPARE_BY_PRODUCTNAME = new Comparator<RowData>() {
public int compare(RowData one, RowData other) {
return one.mProductName.compareTo(other.mProductName);
}
};
}
and i have taken a custom adapter which extends ArrayAdapter<RowData>.
My sorting code i have written in onCreate() is as follows,
Collections.sort(data, RowData.COMPARE_BY_PRODUCTNAME);
adapter = new CustomAdapter(this, R.layout.list,R.id.title, data);
setListAdapter(adapter);
adapter.notifyDataSetChanged();
I have to use a custom adaptor because to show price of the product as well as quantity in hand of the product
the data which is a vector of type RowData I am getting after debugging in sorted order of the product Name which I want but when bind to the ListView it is not displaying in sorted order.
I am new to android please help me.
Thanks Alex Lockwood, I am using custom adapter of type ArrayAdaptor<Class>.
In my onCreate() method I am implementing sorting method like the below,
adapter = new CustomAdapter(this, R.layout.list,R.id.title, data);
adapter.sort(new Comparator<RowData>() {
public int compare(RowData arg0, RowData arg1) {
return arg0.mProductName.compareTo(arg1.mProductName);
}
});
setListAdapter(adapter);
In my customAdaptor class, I have to override sort like this,
public void sort(Comparator<? super RowData> comparator) {
// TODO Auto-generated method stub
super.sort(comparator);
}
Please help me if you can modify the above code or suggest me your code.
Thanks in advance.