开发者

Apply Sorting in adapter in Grid Layout

开发者 https://www.devze.com 2023-04-11 23:18 出处:网络
I have a Grid View in my Android Application in which Images are fetched from xml file and are set and viewed in the gridview. Some images from the xml file have important markings.

I have a Grid View in my Android Application in which Images are fetched from xml file and are set and viewed in the gridview. Some images from the xml file have important markings. I want to show that marked images in the beginning of the grid. For that I think I have to apply sorting. Can anyone suggest me how to do sorting of images? Thanks开发者_运维百科 in Advance.


I assume that you have a list of images which you are binding to adapter. If you have a list of images and want to sort that you need to implement custom Comparator for sorting that array(list).

here is the example of custom Comparator...

public Comparator<TheaterInfo> ComparatorByDistance = new Comparator<TheaterInfo>() {

    public int compare(TheaterInfo object1, TheaterInfo object2) {
        if (object1.getDistance().length() > 0
                && object2.getDistance().length() > 0) {
            String[] d1 = object1.getDistance().replace(" ", "#").split("#");
            String[] d2 = object2.getDistance().replace(" ", "#").split("#");
            float o1 = Utils.ConvertToFloat(d1.length > 1 ? d1[0] : "0.0");
            float o2 = Utils.ConvertToFloat(d2.length > 1 ? d2[0] : "0.0");
            if (o1 == o2)
                return 0;
            return o1 > o2 ? 1 : -1;
        }
        else
        {
            return object1.getDistance().length() > 0 ? 1 : -1;
        }
    }
};

And you can use this Comparator by...

Collections.sort(your_list_type_object, ComparatorByDistance);

Thanks.


Make a list of your elements and use

Collections.sort(your_list_here);

This should sort your items in the list

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号