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

前端 未结 2 462
广开言路
广开言路 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() {
        public int compare(RowData arg0, RowData arg1) {
            return arg0.mProductName.compareTo(arg1.mProductName);
        }
    });
    
    setListAdapter(adapter);
    adapter.notifyDataSetChanged();
    

提交回复
热议问题