1

I'm new on android programming, and still learning about its concept, I'm currently building an app that will get some data from online database and stored it in ArrayList> Type, then I will show the data,

I've successfully get the data from database and successfully showed it on ListView as well,, and now I want to sort the data based on its date (there is one date value stored in the hashmap),

I've read how to do it in these question :

How to sort data of ArrayList of hashmap on The Basis of Date

I don't really get the concept and still have no idea how it could be worked with my current code. Hope you could help me with my code,

This is my code :

public class Notification extends Activity {

userSessionManager session;
String Username, clickedId, clickedTitle, toastMessage;
String urlUpdateGroupConfirmation, urlGetNotif;
JSONParser jsonParser;
ProgressDialog pd;
JSONArray jsonArray = null;
private ArrayList<HashMap<String, String>> whatsNew;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.notification);

    getActionBar().setDisplayShowHomeEnabled(false);
    sessionAndDeclaration();
    new AttemptParseNotification().execute();
}

private void sessionAndDeclaration() {
    // TODO Auto-generated method stub
    session = new userSessionManager(getApplicationContext());
    HashMap<String, String> user = session.getUserDetails();
    Username = user.get(userSessionManager.KEY_USERNAME);
    myIP ip = new myIP();
    String publicIp = ip.getIp();
    String thisPhp = "viewMyNotification.php";
    String updateGConf = "doUpdateGroupDetail.php";

    urlGetNotif = publicIp + thisPhp;
    urlUpdateGroupConfirmation = publicIp + updateGConf;
    jsonParser = new JSONParser();
    whatsNew = new ArrayList<HashMap<String, String>>();

}

class myMapComparator implements Comparator<Map<String, String>> {

    @Override
    public int compare(Map<String, String> lhs, Map<String, String> rhs) {
        // TODO Auto-generated method stub
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        try {
            return df.parse(lhs.get("date")).compareTo(
                    df.parse(rhs.get("date")));

        } catch (ParseException e) {
            throw new IllegalArgumentException(e);
        }
    }

}

class AttemptParseNotification extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        // TODO Auto-generated method stub
        super.onPreExecute();
        pd = new ProgressDialog(Notification.this);
        pd.setIndeterminate(false);
        pd.setCancelable(true);
        pd.setMessage("Loading...");
        pd.show();
    }

    @Override
    protected Boolean doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        int success = 0;
        try {
            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("Username", Username));
            Log.d("Request!", "Passing Username to server");
            JSONObject json = jsonParser.makeHttpRequest(urlGetNotif,
                    "POST", params);
            success = json.getInt("success");
            if (success == 1) {
                Log.d("Response", "Getting todays");
                jsonArray = json.getJSONArray("array");
                try {
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject c = jsonArray.getJSONObject(i);
                        String newId = c.getString("id");
                        String newType = c.getString("type");
                        String newTitle = c.getString("title");
                        String newDisplayed = newTitle + "(" + newType
                                + ")";
                        String newDate = c.getString("date");
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("id", newId);
                        map.put("title", newTitle);
                        map.put("date", newDate);
                        map.put("type", newType);
                        map.put("displayed", newDisplayed);
                        whatsNew.add(map);
                        Collections.sort(whatsNew, new myMapComparator());

                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        pd.dismiss();
        if (whatsNew.size() > 0) {

            viewNews();
        } else {
            Toast.makeText(getBaseContext(), "No new notification",
                    Toast.LENGTH_LONG).show();
        }
    }

}

public void viewNews() {
    // TODO Auto-generated method stub
    ListView lv = (ListView) findViewById(R.id.lv_notif);
    ListAdapter adapter = new SimpleAdapter(this, whatsNew,
            R.layout.notificationlist_item, new String[] { "title", "type",
                    "date" }, new int[] { R.id.title_notif,
                    R.id.type_notif, R.id.date_notif });
    lv.setAdapter(adapter);

}
}
Community
  • 1
  • 1
redRabbit
  • 79
  • 10
  • What is wrong with your code? – Jens Feb 01 '15 at 12:59
  • Is it working or not? Or you want to know why its working? And where are you calling sort on your list? – SMA Feb 01 '15 at 13:00
  • It seems it doesn't return or sort the hashmap, Wait, I will upload my logcat, thankss – redRabbit Feb 01 '15 at 13:02
  • Please provide more details..and one suggestion, instead of using `ArrayList`, use `TreeSet` it will keep your datetime object sorted at all time. – Sufiyan Ghori Feb 01 '15 at 13:02
  • @SufiyanGhori is treeset always sort date automatically? – redRabbit Feb 01 '15 at 13:09
  • @redRabbit If you used a TreeSet instead of an ArrayList, you won't need to do "Collections.sort" yourself. Just create the tree set with an instance of the comparator, for example "new TreeSet>(new myMapComparator()" – BretC Feb 01 '15 at 13:10
  • @Bret under what situation I should use TreeSet instead of ArrayList? what is the benefit if I use treeset? as sufiyan mentioned before about treelist and datetime object, is treelist is always be a better option for storing a datetime object? – redRabbit Feb 01 '15 at 13:15
  • @redRabbit, please read this answer on why you should use `TreeSet` http://stackoverflow.com/questions/18760752/difference-between-collections-sort-and-getting-a-sorted-collection-by-adding – Sufiyan Ghori Feb 01 '15 at 13:24
  • @redRabbit I'd use a TreeSet instead of an ArrayList if I wanted to do lots of "contains" operations. If you had 1,000,000 items in an ArrayList, then "contains" in the worse case would have to search through all 1,000,000 items. A Set would do this in constant time. I would usually use a normal HashSet or something if I didn;t want the set to be ordered – BretC Feb 01 '15 at 14:56

2 Answers2

2

As I suggested to use TreeSet instead of Collections.sort,

here is the rough example,

public static Set<HashMap<String, String>> mySet = new TreeSet<>(new Comparator<HashMap<String, String>>() {
        @Override
        public int compare(HashMap<String, String> o1, HashMap<String, String> o2) {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

            try {
                return df.parse(o1.get("date")).compareTo(
                        df.parse(o2.get("date")));

            } catch (ParseException e) {
                throw new IllegalArgumentException(e);
            }       }
    }); 

The difference b/w Collections.sort and TreeSet is that a TreeSet keeps your data sorted at all times while the Collections.sort() method sorts it when you call the method on your Set.

for example, if you add data in it, i.e, mySet.add(yourData); , it will be added in sorted order.

Sufiyan Ghori
  • 18,164
  • 14
  • 82
  • 110
1

After the loop that adds all of the data to the "whatsNew" list, you need to call your comparator to sort the list.

Add this line to "doInBackground" before "return null"...

Collections.sort(whatsNew, new myMapComparator());

(side note: as you may already know, in Java the convention is to start a class name with a capital letter)

BretC
  • 4,141
  • 13
  • 22
  • import is java.util.Collections and also you could make your comparator class "static" as it does not use any state from the enclosing class – BretC Feb 01 '15 at 13:03
  • thank you very much, I've called it in my postExecute, and it seems I miss something when I pass the data, it's solved – redRabbit Feb 01 '15 at 13:11