Saturday, 10 August 2013

How can retrieve only name on contact in android

How can retrieve only name on contact in android

I use a below code, but I need only contact name and this code retrieve
all data from contact, Facebook ,Nimbuzz and .... Cursor cursor =
getContentResolver().query( ContactsContract.Contacts.CONTENT_URI, null,
null, null, null); selectedRow = new int[cursor.getCount()];
data = new String[cursor.getCount()][12];
while (cursor.moveToNext()) {
//numberPhone = 0;
contactId = cursor.getString(cursor
.getColumnIndex(ContactsContract.Contacts._ID));
Uri contactUri = ContentUris.withAppendedId(Contacts.CONTENT_URI,
Long.parseLong(contactId));
Uri dataUri = Uri.withAppendedPath(contactUri,
Contacts.Data.CONTENT_DIRECTORY);
try {
Cursor nameCursor = getContentResolver()
.query(dataUri,null,Data.MIMETYPE + "=?",new String[]
{ StructuredName.CONTENT_ITEM_TYPE }, null);
int indexDisplayName =
nameCursor.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME);
nameCursor.moveToFirst();
do {
String firstName = nameCursor.getString(nameCursor
.getColumnIndex(Data.DATA2));
String lastName = nameCursor.getString(nameCursor
.getColumnIndex(Data.DATA3));
Arrayename.add(firstName+" "+lastName);
//String display = nameCursor.getString(indexDisplayName);
//Log.d("display name", "display name is: " + display);
} while (nameCursor.moveToNext());
nameCursor.close();
} catch (Exception e) {
Toast.makeText(this, "Error Happend While Reading Names",
Toast.LENGTH_SHORT).show();
}
}
how can fetch only contact data?

No comments:

Post a Comment