3

I have a custom ArrayAdapter<Summary> which holds a list of events. There are duplicate values in the List<Summary>, so I'm trying to put the values of List<Summary> to LinkedHashSet<Summary> but this displays a blank page.

How do I convert custom ArrayList to LinkedHashSet to get unique data?

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) ...
input
  • 7,503
  • 25
  • 93
  • 150
  • 1. You have a syntax error in your code (at the foreach). 2. Why do you add a `Summary` to your `ArrayList` and then add the *whole* list to the `LinkedHashSet`? – nkr Feb 03 '13 at 15:53
  • @nkr, 1. I corrected the error. It was only here in the code. 2. How else should I add `Summary` to `LinkedHashSet`? – input Feb 03 '13 at 16:10
  • `Summary summary = new Summary(...); summaries.add(summary); listToSet.add(summary);` – nkr Feb 03 '13 at 22:00
  • `listToSet.add(summary);` gives the error that `add` is not applicable for Summary – input Feb 03 '13 at 22:15
  • @input. What was the solution to you problem? Were was the problem? Was my answer of any help? – Witold Kaczurba Oct 31 '16 at 07:49

1 Answers1

1

You need to ensure that the class you are putting into Set properly overrides Equals and hashCode functions.

Lets have a look at case where hashCode is not overriden:

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);

    }
}

This will produce:

ar: [Abc: a, Abc: a, Abc: a] LinkedHashSet: [Abc: a, Abc: a, Abc: a]

even though equals are properly implemented. I believe you may want to double-check proper implementation of both HashCodes and Equals.

Witold Kaczurba
  • 9,845
  • 3
  • 58
  • 67