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;
}
}
答案 0 :(得分:2)
您可以直接使用收藏夹。
Collections.sort(contactList, String.CASE_INSENSITIVE_ORDER);
答案 1 :(得分:0)
您可以使用排序比较器对列表进行排序。例如:
Collections.sort(arrayList, Comparator_NAME);
private Comparator<? super String> Comparator_NAME = new Comparator<String>() {
@Override
public int compare(String arg0, String arg1) {
return arg0.toLowerCase().compareTo(arg1.toLowerCase());
}
};