Categorizes different types of objects into a list
I
have the following issue and I’m not quite sure how to fix it. I request data from several different servers, each returning a list of objects of different types (each with its own specific unique POJO object type) – but what all of these object lists have in common is that all objects have a date parameter. Then I need to display all the objects in the list as a huge mixed list for the user to use, all sorts of various objects sorted by date.
I
made an adapter that extends the baseadapter, and I’ve passed all the object array lists to that adapter – but how can I sort all these object lists by date and show them all together? I could create a “super object” with properties from all the other objects and then create a sorted array for this super object to pass to the adapter, but that seems like a confusing and rough solution. Is there a more elegant way to do this?
public class someAdapter extends BaseAdapter
{
...
public someAdapter(ArrayList<ObjectOne> objectOneArray, ArrayList<ObjectTwo> objectTwoArray) {
if (objectOneArray != null) {
this.localObjectOneList.addAll(objectOneArray);
}
...
}
}
Object type example:
public class ObjectOne implements Serializable {
private String date;
private String someFieldOne;
private String someFieldTwo;
...
getters and setters
...
}
To reiterate – the final list must be a combination of all the different object types in date order.
Solution
Without a large pipeline associated with it, the way you initially expected to have a superclass would be fine.
A better approach is to assume that all POJOs have a getDate() method.
- Declare a method getDate() to retrieve the date object from any POJO.
- Declare
Comparator
comparison dates Sort the objects
If the POJO does not have a getter, i.e. the getDate() method, replace getDateFromMethod() with
getDateFromProperty
()
inthe comparator
public class Sorter{ public void sortList() { Collections.sort(objList, comparator); Now consume / print your objList to get sorted by date } ArrayList<Object> objList = new ArrayList<Object>(); Comparator<Object> comparator = new Comparator<Object>() { @Override public int compare(Object object1, Object o2) { Date o1Date = getDateFromMethod(object1); Date o2Date = getDateFromMethod(o2); if (o1Date != null && o2Date != null) { if (o1Date.before(o2Date)) return -1; else if (o1Date.after(o2Date)) return 1; else return 0; } else return -1; } }; public Date getDateFromMethod(Object obj) { Date date = null; String getDate = "getDate"; try { Method method = obj.getClass().getMethod(getDate, (Class<?>[]) null); if (method != null) date = (Date) method.invoke(obj, (Object[]) null); } catch (NoSuchMethodException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { TODO Auto-generated catch block e.printStackTrace(); } return date; }public Date getDateFromProperty(Object obj) { Date date=null; try { Field dateField= obj.getClass().getField("date"); if(dateField!=null) date=(Date) dateField.get(obj); } catch (NoSuchFieldException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { TODO Auto-generated catch block e.printStackTrace(); } return date; }
Now suppose you have the following Obj1, Obj2, and Obj3, use these POJOS to create objects and populate objList and test the solution.
Class obj1
{
Build number;
integer name;
Date date;
public Date getDate()
{
Return date;
}
Public int getId()
{
Returns the ID;
public void setId(int id)
{
this.id = id;
}
public int getName()
{
return name;
}
public void setName(int name)
{
this.name = name;
}
Class obj2
{
integer sequence;
internal location;
Date date;
public Date getDate()
{
Return date;
}
public int getSequence()
{
return sequence;
public void setSequence(int sequence)
{
this.sequence = sequence;
}
public int getLocation()
{
return location;
}
public void setLocation(int location)
{
this.location = location;
}
Class Obj3
{
integer type;
internal sources;
Date date;
public Date getDate()
{
Return date;
public void setDate
{
this.date = date;
public int getType()
{
return type;
}
public void setType(int type)
{
this.type = type;
}
public int getOrigin()
{
return origin;
}
public void setOrigin(int origin)
{
this.origin = origin;
}