TextView içine kriterlere uyan telefondaki kişiler listesini renklendirerek eklemek.
package com.example.telefonlistesi;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.text.Html;
import android.widget.TextView;
public class TelefonListesi extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv = (TextView) findViewById(R.id.txt1);
getContacts(tv);
}
public void getContacts(TextView tv) {
String[] projection = null; // new String[]{
// ContactsContract.Contacts.DISPLAY_NAME,
// ContactsContract.Contacts.PHOTO_ID };
// DISPLAY_NAME LIKE ? AND HAS_PHONE_NUMBER = ?
String selection = ContactsContract.Contacts.DISPLAY_NAME
+ " LIKE ? AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER
+ " = ?";
// Adı A ile başlayan ve telefon numarası olanlar
String[] selectionArgs = new String[] { "A%", "1" };
// Sıralama _ID ye göre tersten olsun
String sort = ContactsContract.Contacts._ID + " DESC";
Cursor cursor = getContentResolver().query(
ContactsContract.Contacts.CONTENT_URI,
projection,
selection,
selectionArgs,
sort);
while (cursor.moveToNext()) {
String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
String hasPhone = cursor.getString(cursor.getColumnIndex("HAS_PHONE_NUMBER"));
String displayName = cursor.getString(cursor.getColumnIndex("display_name"));
tv.append("\nID: " + contactId);
tv.append("\tDISP. NAME: " + displayName);
if (Integer.parseInt(hasPhone) > 0) {
Cursor crPhones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId,
null,
null);
while (crPhones.moveToNext()) {
String phoneNumber = crPhones.getString(
crPhones.getColumnIndex(
ContactsContract.CommonDataKinds.Phone.NUMBER
)
);
tv.append(Html.fromHtml("\tPHONE: " + phoneNumber+""));
}
crPhones.close();
}
}
}
}