2013-05-15 90 views
2

我有一個ArrayList的對象。Android中的ArrayList排序問題

ArrayList<Item> blog_titles = new ArrayList<Item>(); 

我要排序在其存儲爲字符串(時間戳在下面的代碼)日期時間值的數據成員中的一個的下降順序ArrayList中。

public class BlogItem implements Item, Comparable<BlogItem> { 

    public final String id; 
    public final String heading; 
    public final String summary; 
    public final String description; 
    public final String thumbnail; 
    public final String timestamp; // format:- 2013-02-05T13:18:56-06:00 
    public final String blog_link; 

    public BlogItem(String id, String heading, String summary, String description, String thumbnail, String timestamp, String blog_link) {  
     this.id = id; 
     this.heading = heading; 
     this.summary = summary; 
     this.description = description; 
     this.thumbnail = thumbnail; 
     this.timestamp = timestamp; // format:- 2013-02-05T13:18:56-06:00 
     this.blog_link = blog_link; 
    } 

    @Override 
    public int compareTo(BlogItem o) { 
     // TODO Auto-generated method stub 
     return this.timestamp.compareTo(o.timestamp); 
    } 

} 

項目是一個通用的接口:

public interface Item { 
    // TODO Auto-generated method stub 
} 

現在,當我試圖有點像ArrayList中:

Collections.sort(blog_titles); 

我收到以下錯誤信息:

Bound mismatch: The generic method sort(List<T>) of type Collections is not applicable for the arguments (ArrayList<Item>). The inferred type Item is not a valid substitute for the bounded parameter <T extends Comparable<? super T>> 

我該如何解決上述錯誤&這是在這種情況下排序ArrayList的正確方法嗎?

+0

請參閱此鏈接this它會幫助你理清類型的數據。 –

+0

我已經實現瞭解決方案中提到的相同概念...但是,我在這裏收到了一個不同的錯誤消息 – Sourav

+0

您得到了什麼類型的錯誤? –

回答

3

您的blog_titles列表是一個列表Item

Item本身不是Comparable,而BlogItem是。

要麼宣佈blog_titles作爲ArrayList<BlogItem>,或使Item延長Comparable

+0

非常感謝您分享信息。 – Narasimha

1
Try this.. 

Collections.sort(blog_titles, new Comparator<BlogItem>() { 

     @Override 
     public int compare(BlogItem lhs, BlogItem rhs) 
     { 
      // TODO Auto-generated method stub 
      return (int)(rhs.timestamp - lhs.timestamp); 
     } 
    }); 
+0

@Sourav在我的回答時間戳的數據類型很長。 – TheFlash