0

i need a little help with my listview / hashmap.

I want to sort my listView by int values (TAG_EPOCH).

During my search on internet i found out that i might need a LinkedHashMap or a TreeSet but i don't understand how to use them with my existing code.

Here is an example of my code:

protected void onCreate(Bundle savedInstanceState) {
    ...
    contactList = new ArrayList<HashMap<String, String>>();
    ...
}

static ArrayList<HashMap<String, String>> contactList;
private static final String TAG_EPOCH = "dateEpoch";

private class GetContacts extends AsyncTask<Void, Void, Void> {
    ...
    @Override
    protected void onPostExecute(Void result) {
        ListView dummyView = (ListView) findViewById(R.id.list);

        mAdapter = new MyCustomAdapter();
        for (int i = 1; i < contactList.size(); i++) {
            HashMap<String, String> c = contactList.get(i);
            mAdapter.addItem(TAG_EPOCH);
        }
        dummyView.setAdapter(mAdapter);
    ...
}
Maurice
  • 792
  • 11
  • 34
  • I guess this should help : >http://stackoverflow.com/questions/8908549/sorting-of-listview-by-name-of-the-product-using-custom-adaptor/8920348#8920348 – vjdhama Apr 09 '14 at 12:00

3 Answers3

4

You can try to implement a Comparator In your class

class Contact implements Comparable<Contact> 

Then add this:

public static final Comparator<Contact> INTEGER_COMPARATOR = new Comparator<Contact>() {
    // Overriding to sort on your integer
    public int compare(Contact c1, Contact c2) {
        return c1.integer - c1.integer;
    }
};

Then sort it using:

Collections.sort(list, Contact.INTEGER_COMPARATOR);
AndyH
  • 447
  • 3
  • 13
0

You should sort your listview?

Set a list of items in the adapter like

ArrayList list = new ArrayList();

// Add values to list

sorting an arraylist is easier with Compare

Sorting ArrayList

Community
  • 1
  • 1
Viswanath Lekshmanan
  • 9,945
  • 1
  • 40
  • 64
0

You can use Collections.sort() methods to sort your ArrayList

Roman Black
  • 3,501
  • 1
  • 22
  • 31