如何在实施Parcelable排序的ArrayList [英] How to sort ArrayList while implementing Parcelable

查看:110
本文介绍了如何在实施Parcelable排序的ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用排序 Col​​lections.sort 方法类数组列表,但没有运气吧。

下面是我的code:

 公共类分类实现Parcelable {
    私人的ArrayList<类别及GT;类别;
    私人最近最近;    公众的ArrayList<类别及GT; getCategories(){
        返回this.category;
    }    公共无效setCategory(ArrayList的<分类和GT;类){
        this.category =类别;
    }    公共近期getRecent(){
        返回this.recent;
    }    公共无效setRecent(近期近期){
        this.recent =最近的;
    }
    保护类别(包裹中){
        如果(in.readByte()== 0×01){
            类别=新的ArrayList<类别及GT;();
            in.readList(类别,Category.class.getClassLoader());
        }其他{
            类别= NULL;
        }
        最近=(最近)in.readValue(Recent.class.getClassLoader());
    }    @覆盖
    公众诠释describeContents(){
        返回0;
    }    @覆盖
    公共无效writeToParcel(DEST包裹,INT标志){
        如果(类别== NULL){
            dest.writeByte((字节)(0×00));
        }其他{
            dest.writeByte((字节)(0×01));
            dest.writeList(类);
        }
        dest.writeValue(近期的);
    }    公共静态最终Parcelable.Creator<类别> CREATOR =新Parcelable.Creator<类别>(){
        @覆盖
        公共分类createFromParcel(包裹中){
            返回新的类别(在);
        }        @覆盖
        公共类别[] newArray(INT大小){
            返回新的类别【尺寸】;
        }
    };
}


解决方案

您也可以使用自定义的比较:

 公共类CategoriesComparator实现比较<类别及GT; {
    @覆盖
    公众诠释比较(分类类别1,类别类别1){
        返回category1.getSomeProperty()的compareTo(category2.getSomeProperty())。
    }
}

当您要比较称之为:

  Col​​lections.sort(yourListCategories,新CategoriesComparator());

希望它帮助!

I am trying to sort the category arraylist with Collections.sort method but have no luck with it.

Here is my code:

public class Categories implements Parcelable {
    private ArrayList<Category> category;
    private Recent recent;

    public ArrayList<Category> getCategories() {
        return this.category;
    }

    public void setCategory(ArrayList<Category> category) {
        this.category = category;
    }

    public Recent getRecent() {
        return this.recent;
    }

    public void setRecent(Recent recent) {
        this.recent = recent;
    }


    protected Categories(Parcel in) {
        if (in.readByte() == 0x01) {
            category = new ArrayList<Category>();
            in.readList(category, Category.class.getClassLoader());
        } else {
            category = null;
        }
        recent = (Recent) in.readValue(Recent.class.getClassLoader());
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if (category == null) {
            dest.writeByte((byte) (0x00));
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeList(category);
        }
        dest.writeValue(recent);
    }

    public static final Parcelable.Creator<Categories> CREATOR = new Parcelable.Creator<Categories>() {
        @Override
        public Categories createFromParcel(Parcel in) {
            return new Categories(in);
        }

        @Override
        public Categories[] newArray(int size) {
            return new Categories[size];
        }
    };
}

解决方案

You can also use custom comparator:

public class CategoriesComparator implements Comparator<Category> {
    @Override
    public int compare(Category category1, Category category2) {
        return category1.getSomeProperty().compareTo(category2.getSomeProperty());
    }
}

When you want to compare call this:

Collections.sort(yourListCategories, new CategoriesComparator());

Hope it helps!

这篇关于如何在实施Parcelable排序的ArrayList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆