How to sort contacts by name?

0

I have the following code and would like to make an ORDER BY name:

try {
        String clsSimPhonename = null;
        String clsSimphoneNo = null;

        Uri simUri = Uri.parse("content://icc/adn");
        Cursor cursorSim = getContentResolver().query(simUri, null,
                null, null, "name ASC");
        while (cursorSim.moveToNext()) {
            clsSimPhonename = cursorSim.getString(cursorSim
                    .getColumnIndex("name"));
            clsSimphoneNo = cursorSim.getString(cursorSim
                    .getColumnIndex("number"));
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    
asked by anonymous 20.10.2014 / 16:05

2 answers

0

You have to make a method for sorting:

public List<Telefone> ordenarTelefones(List<Telefone> lista)
        throws Exception {

    Collections.sort(lista, new Comparator<Telefone>() {

        @Override
        public int compare(Telefone tel1, Telefone tel2) {
            return tel1.getNome().compareTo(tel2.getNome());

        }
    });

    return lista;
}
    
22.10.2014 / 00:34
0

Using the last parameter in query , change your Cursor to something like this:

Cursor cursor = getContentResolver.query(simUri, null, null, null, "name ASC");
    
20.10.2014 / 17:33