Sorting of ListView by Name of the Product using Custom adaptor?

前端 未结 2 461
广开言路
广开言路 2020-12-30 12:11

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 cl         


        
相关标签:
2条回答
  • 2020-12-30 12:55

    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();
    
    0 讨论(0)
  • 2020-12-30 13:08

    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);
        }
    });
    
    0 讨论(0)
提交回复
热议问题