Java 如何在实现Parcelable时对ArrayList进行排序
我正在尝试使用Java 如何在实现Parcelable时对ArrayList进行排序,java,android,arraylist,Java,Android,Arraylist,我正在尝试使用Collections.sort方法对类别arraylist进行排序,但没有成功 这是我的密码: public class Categories implements Parcelable { private ArrayList<Category> category; private Recent recent; public ArrayList<Category> getCategories() { return
Collections.sort
方法对类别arraylist进行排序,但没有成功
这是我的密码:
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];
}
};
}
公共类类别实现可包裹{
私有数组列表类别;
私人最近;
公共阵列列表getCategories(){
返回此.category;
}
公共无效集合类别(ArrayList类别){
this.category=类别;
}
公共最近的getRecent(){
返回此文件。最近的;
}
公共空间最近(最近){
this.recent=最近的;
}
受保护类别(包裹中){
if(in.readByte()==0x01){
类别=新的ArrayList();
in.readList(category,category.class.getClassLoader());
}否则{
类别=空;
}
最近=(最近)in.readValue(最近的.class.getClassLoader());
}
@凌驾
公共int描述内容(){
返回0;
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
如果(类别==null){
目标写入字节((字节)(0x00));
}否则{
目标写入字节((字节)(0x01));
目的书面列表(类别);
}
目的地书面价值(最近);
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
@凌驾
公共类别createFromParcel(地块中){
返回新类别(在中);
}
@凌驾
公共类别[]新数组(整数大小){
返回新类别[大小];
}
};
}
Collections.sort(yourListHere,newcomparator(){
@凌驾
公共整数比较(类别lhs、类别rhs){
//你的排序逻辑在这里
返回0;
}
});
希望这有帮助。Collections.sort(yourListHere,newcomparator()){
@凌驾
公共整数比较(类别lhs、类别rhs){
//你的排序逻辑在这里
返回0;
}
});
希望这有帮助。您也可以使用自定义比较器:
public class CategoriesComparator implements Comparator<Category> {
@Override
public int compare(Category category1, Category category2) {
return category1.getSomeProperty().compareTo(category2.getSomeProperty());
}
}
希望有帮助 您也可以使用自定义比较器:
public class CategoriesComparator implements Comparator<Category> {
@Override
public int compare(Category category1, Category category2) {
return category1.getSomeProperty().compareTo(category2.getSomeProperty());
}
}
希望有帮助 你有没有试过在你的分类课上实现Comparable@user2004685我以前从未使用过
委托
。你能帮我吗?@user2004685我不熟悉Android编程,但为什么不呢?你能链接一些相关信息吗?@user2004685上次我读到你能。您不能扩展多个类,您可以实现许多接口。@RubioRic是的,您是对的。好像我还在宿醉;你有没有试过在你的分类课上实现可比性@user2004685我以前从未使用过委托
。你能帮我吗?@user2004685我不熟悉Android编程,但为什么不呢?你能链接一些相关信息吗?@user2004685上次我读到你能。您不能扩展多个类,您可以实现许多接口。@RubioRic是的,您是对的。好像我还在宿醉;p是否要将新的自定义类粘贴到category类的上方?不,只需在另一个文件中创建新类,并在其中对数组调用进行排序Collections.sort(yourListCategories,new CategoriesComparator())
我也这么做了,现在它说无法解析排序符号
。我是否要将新的自定义类粘贴到category类之上?不,只需在另一个文件中创建新类,并在其中对数组调用集合进行排序。排序(yourListCategories,new CategoriesComparator())代码>我也这么做了,现在它说无法解析排序符号
。
Collections.sort(yourListCategories, new CategoriesComparator());