2013-02-03 139 views
1

我有一個自定義ArrayAdapter<Summary>,它包含一系列事件。在List<Summary>中有重複的值,所以我試圖將List<Summary>的值設置爲LinkedHashSet<Summary>,但這會顯示一個空白頁。將自定義ArrayList轉換爲LinkedHashSet

如何將自定義ArrayList轉換爲LinkedHashSet以獲取唯一的數據?

Main.java:

LinkedHashSet<Summary> listToSet = new LinkedHashSet<Summary>(); 
final List<Summary> summaries = new ArrayList<Summary>(); 

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
    View v = inflater.inflate(R.layout.events_summary, container, false); 

    ....... 

    setListView(month, year, date_value); 
    summaryAdapter = new SummaryAdapter(this.getActivity().getApplicationContext(), R.layout.listview_item_row, listToSet); 

    calendarSummary = (ListView) v.findViewById(R.id.calendarSummary); 
    calendarSummary.setAdapter(summaryAdapter); 

    return v; 
} 

public void setListView(int month, int year, int dv) { 

     events = new HolidayEvents(); 
     _calendar = Calendar.getInstance(Locale.getDefault()); 
     int totalDays = _calendar.getActualMaximum(Calendar.DAY_OF_MONTH); 

     for(int i = 1; i <= totalDays; i++){ 
      if(isHoliday(i, month, year, dv)) 
      { 
       date = i + " " + getMonthForInt(month-1) + " " + year; 

       for (Event event : events.eventDetails(this, month, i)) 
       { 
         summaries.add(new Summary(date, event.eventdetails)); 
         listToSet.addAll(summaries); 

       } 
      } 
     } 
} 

ArrayAdapter.java:

public class SummaryAdapter extends ArrayAdapter<Summary>{ 

    Context context; 
    int layoutResourceId; 
    LayoutInflater mInflater; 
    LinkedHashSet<Summary> list = null; 
    List<Summary> data = null; 


    public SummaryAdapter(Context context, int layoutResourceId, LinkedHashSet<Summary> summaries) { 
     super(context, layoutResourceId); 
     this.layoutResourceId = layoutResourceId; 
     this.context = context; 
     this.list = summaries; 
     data = new ArrayList<Summary>(list); //converting LinkedHashSet to List 
     mInflater = LayoutInflater.from(context); 
    } 

....rest of the code retrieving data by using data.get(position) ... 
+0

1.您的代碼中存在語法錯誤(在foreach中)。 2.爲什麼給'ArrayList'添加一個'Summary',然後將* whole *列表添加到'LinkedHashSet'? – nkr

+0

@nkr,1.我糾正了錯誤。這只是在代碼中。 2.我還應該如何向'LinkedHashSet'添加'Summary'? – input

+0

'摘要摘要=新摘要(...); summaries.add(摘要); listToSet.add(summary);' – nkr

回答

0

你需要確保你正在付諸實施的設置適當類重寫equals和hashCode功能。

讓我們來看看情況下的hashCode未重寫:

import java.util.*; 

public class Example { 

    public static class Abc { 
     protected String s; 
     public Abc(String s) { 
      this.s = s; 
     } 

     @Override 
     public boolean equals(Object other) { 
      if (other instanceof Abc) { 
       return ((Abc) other).s.equals(this.s); 
      } 
      return false; 
     } 

     @Override 
     public int hashCode() { 
      return (int) (Math.random() * Integer.MAX_VALUE); 
     } 

     public String toString() { 
      return "Abc: " + this.s; 
     } 
    } 

    public static void main(String[] args) { 

     ArrayList<Abc> ar = new ArrayList<>(); 

     ar.add(new Abc("a")); 
     ar.add(new Abc("a")); 
     ar.add(new Abc("a")); 

     LinkedHashSet<Abc> lhs = new LinkedHashSet<>(ar); 

     System.out.println("ar: " + ar); 
     System.out.println("LinkedHashSet: " + lhs); 

    } 
} 

這將產生:

AR:[美國廣播公司:一,美國廣播公司:一,美國廣播公司:一個] LinkedHashSet :[Abc:a,Abc:a,Abc:a]

即使等於正確實施。 我相信你可能想仔細檢查HashCodes和Equals的正確實現。