在android中对联系人列表进行排序

在android中对联系人列表进行排序

package com.example.readcontaectlist;

import java.util.ArrayList;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

    ListView listView;
    ArrayAdapter<String> adapter;
    ArrayList<String>  arrayList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listView=(ListView) findViewById(R.id.listView1);
        arrayList=new  ArrayList<String>();
        arrayList=getNumber(getContentResolver());
        adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(adapter);
    }

    public ArrayList<String> getNumber(ContentResolver cr)
    {
        ArrayList<String> contactList=new ArrayList<String>();
        Cursor phones = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
        // use the cursor to access the contacts
        while (phones.moveToNext())
        {
            String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
        // get display name
            String  phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
            contactList.add(name+"  "+phoneNumber);
        }
        return contactList;
    }
}

最佳答案

您可以为此简单地使用Collections。

Collections.sort(contactList, String.CASE_INSENSITIVE_ORDER);

关于java - 在android中对联系人列表进行排序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/31218269/

10-11 00:48