I am making gallery app and I want to add sorting to it I can sort item at run time by using Comparator
but problem is whenever I quit app the list came form database again and all the list is unsorted I want to give option in my app to sort by date,size, name etc can you guys help how can i ahcevie this and remmeber user's choice if he next time run the app I have also read about sorted-list please tell me what is solution to my problem or any sample code currently I am doing this like this to sort items
public static final Comparator<MediaFileObject> byName=new Comparator<MediaFileObject>() {
@Override
public int compare(MediaFileObject o1, MediaFileObject o2) {
return o1.getAddedDate().compareTo(o2.getAddedDate());
}
};
and i call this in my fragment
Collections.sort(list,MediaFileObject.byName);
galleryAdapter.notifyDataSetChanged()
this is working fine but i want to remember user sorting choice please someone help what should i do?
can i achieve this by using SortedList
? or any sample code please
This help you to sort list, based on Name and Date.
if (theArrayList.size() > 0) {
if (myUserSelectedSortedType == SORT_BY_NAME) {
sortListByName(theArrayList);
} else if (myUserSelectedSortedType == SORT_BY__DATE) {
sortListByDate(theArrayList);
}
myAdapter = new Adapter(MainActivity.this, theArrayList);
myListView.setAdapter(myAdapter);
}
private void sortListByName(ArrayList<CustomerEvents> theArrayListEvents) {
Collections.sort(theArrayListEvents, new EventDetailSortByName());
}
private class EventDetailSortByName implements java.util.Comparator<CustomerEvents> {
@Override
public int compare(CustomerEvents customerEvents1, CustomerEvents customerEvents2) {
String name1, name2;
name1 = customerEvents1.getMyCustomerName().toLowerCase().trim();
name2 = customerEvents2.getMyCustomerName().toLowerCase().trim();
return name1.compareTo(name2);
}
}
private void sortListByDate(ArrayList<CustomerEvents> theArrayListEvents) {
Collections.sort(theArrayListEvents, new EventDetailSortByDate());
}
private class EventDetailSortByDate implements java.util.Comparator<CustomerEvents> {
@Override
public int compare(CustomerEvents customerEvents1, CustomerEvents customerEvents2) {
Date DateObject1 = StringToDate(customerEvents1.getMyDOB());
Date DateObject2 = StringToDate(customerEvents2.getMyDOB());
Calendar cal1 = Calendar.getInstance();
cal1.setTime(DateObject1);
Calendar cal2 = Calendar.getInstance();
cal2.setTime(DateObject2);
int month1 = cal1.get(Calendar.MONTH);
int month2 = cal2.get(Calendar.MONTH);
if (month1 < month2)
return -1;
else if (month1 == month2)
return cal1.get(Calendar.DAY_OF_MONTH) - cal2.get(Calendar.DAY_OF_MONTH);
else return 1;
}
}
public static Date StringToDate(String theDateString) {
Date returnDate = new Date();
if (theDateString.contains("-")) {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm");
try {
returnDate = dateFormat.parse(theDateString);
} catch (ParseException e) {
SimpleDateFormat altdateFormat = new SimpleDateFormat("dd-MM-yyyy");
try {
returnDate = altdateFormat.parse(theDateString);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
} else {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm");
try {
returnDate = dateFormat.parse(theDateString);
} catch (ParseException e) {
SimpleDateFormat altdateFormat = new SimpleDateFormat("dd/MM/yyyy");
try {
returnDate = altdateFormat.parse(theDateString);
} catch (ParseException ex) {
ex.printStackTrace();
}
}
}
return returnDate;
}
You can create a options list like sort by Date, Size, Name etc. Make one of the options as the default option and save to sharedpreference. when a user changes the sorting type make the selected option as the default.
When you read the data from the database, as per the selected sorting option, apply the collection sort.
I believe no code snippet require for that.