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) ...
1.您的代碼中存在語法錯誤(在foreach中)。 2.爲什麼給'ArrayList'添加一個'Summary',然後將* whole *列表添加到'LinkedHashSet'? – nkr
@nkr,1.我糾正了錯誤。這只是在代碼中。 2.我還應該如何向'LinkedHashSet'添加'Summary'? – input
'摘要摘要=新摘要(...); summaries.add(摘要); listToSet.add(summary);' – nkr