7

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.

6
  • Make sure that your "CustomAdapter" is able to display a normal array or ArrayList. When this works - you should also be able to display a sorted data set. Commented Jan 18, 2012 at 10:34
  • you dont need to use notifyDataSetChanged() method, when you are setting new adapter to list. Just paste your adapter code and activity code. Commented Jan 18, 2012 at 10:36
  • Thanks Copa Could you please provide me the code for the same or could you please help me using the code Commented Jan 18, 2012 at 12:10
  • Thanks jitendra sharma but it is not working even after removal of notifyDataSetChanged() method Commented Jan 18, 2012 at 12:11
  • please use proper capitalization and try to format your code better next time. people are much more likely to help if your code is easy to read :). Commented Jan 19, 2012 at 14:40

2 Answers 2

18

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();
Sign up to request clarification or add additional context in comments.

3 Comments

hanks Alex Lockwood please see my below query
thanks "Alex Lockwood", now its working the problem i have encounter in getView() method there I am able to find my Vector which is sorted earlier now thats working thank you for your help!
plz see this how to sort listview by name
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.