2016-03-25 113 views
0

我有傳遞給標籤的類別Arraylist。標籤獲取類別名稱並進行設置。如何對類別Arraylist進行排序?

private List<Category> mCategories; 
private LayoutInflater mInflater; 

public LeftMenuAdapter(Context context, List<Category> categories) { 
    mCategories = categories; 
    mInflater = (LayoutInflater) context 
      .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
} 
Category category = mCategories.get(position - 3); 
holder.label.setText(category.getName()); 

分類等級:

public class Category implements Parcelable { 
private List<String> images; 
private String name; 

public List<String> getImages() { 
    return this.images; 
} 

public void setImages(List<String> images) { 
    this.images = images; 
} 

public String getName() { 
    return this.name; 
} 

public void setName(String name) { 
    this.name = name; 
} 

protected Category(Parcel in) { 
    if (in.readByte() == 0x01) { 
     images = new ArrayList<String>(); 
     in.readList(images, String.class.getClassLoader()); 
    } else { 
     images = null; 
    } 
    name = in.readString(); 
} 

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

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

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

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

}

我使用的比較,但它說Cannot resolve symbol sort。我怎樣才能按名稱對類別進行排序?

+1

你可以發表你的 '類別' 類,它具有實現比較? –

+0

分類類是不同的,我問的類別也不同,看你有沒有使用名單不是名單 ... !! –

+1

您想按類別名稱對列表進行排序嗎? –

回答

2

你需要從ArrayList中創建自定義比較

public class CustomComparator implements Comparator<MyObject> { 
    @Override 
    public int compare(MyObject o1, MyObject o2) { 
     return o1.getStartDate().compareTo(o2.getStartDate()); 
    } 
} 

,並比較你的對象:

Collections.sort(arrayList, new CustomComparator()); 
2

您必須在Category類中實現Comparable接口並覆蓋compareTo()方法。

1

所以在這裏你的問題是那種不知道做什麼用class.you有創建類別,但你必須寫一個方法,告訴究竟什麼是必須排序。 如果你的類包含字符串,然後使用比較器,並根據起始字符或任何你喜歡的類型進行排序 與int和所有東西類似。 目前,如果您打電話給您的名單排序,它不知道如何處理您的課程。

3

定義一個類同樣,

class SortByNameCategory implements Comparator<Category>{ 

    @Override 
    public int compare(Category c1, Category c2) { 
     return (c1.getName() != null ? c1.getName().compareTo(c2.getName())).compareTo(c2.getName() : 0); 
    } 

} 

...在別的地方..

List<Category> listofCategory = new ArrayList<Category>(); 
// populate this listofCategory as per your wish... 

    java.util.Collections.sort(listofCategory , new SortByNameCategory()); // this will sort your listofCategory by Category's Name base 
+0

它說'不能解決符號排序' –

+0

正是我寫的回答,因爲'排序'是一個靜態方法的集合。所以請檢查正確。如果你還沒有導入任何東西,那麼編寫完全限定的名字,就像我寫的,java.util.Collections.sort ... –

+0

謝謝。第一個代碼塊有語法錯誤。 –

3
public class CategoryComparator implements Comparator<Category> { 
     @Override 
     public int compare(Category o1, Category o2) { 
      if(o1.getName()!=null && o2.getName()!=null){ 
       return o1.getName().compareToIgnoreCase(o2.getName()); 
      }else{ 
       return 0; 
      } 
     } 
    } 
+0

我將代碼粘貼到類別類中,但它不會按名稱對其進行排序。我也將它粘貼在類別類之上而不使用「public」,也沒有工作。 –

+1

它是一個單獨的類,您不必將其放入類別類中。如果你想像這樣的東西實現Comparable接口。 – itpr

相關問題