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.
If you're using an ArrayAdapter
, you can call sort
on the adapter and pass a Comparator
. How you implement your Comparator
is up to you.
Edit I:
You shouldn't need to override sort
... the ArrayAdapter
class implements that for you. All you need to do is create your own Comparator
that will sort the generic type objects the way you want them to appear. Also, if you sort the list during runtime, you might need to notify the application that the data set has changed. Your code should work correctly if you use this approach.
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);
adapter.notifyDataSetChanged();
If you want to sort locale-sensitive Strings use the Collator Class e.g. in german: A, Ä, B
final Collator col = Collator.getInstance();
adapter.sort(new Comparator<RowData>() {
public int compare(RowData arg0, RowData arg1) {
return col.compare(arg0.mProductName, arg1.mProductName);
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With