Hi, Abhilash !

I realized your requirements yesterday finally and edited some lines of code.

The following program works well on my Android real device(Android 2.3.6, Galaxy S, 2010, Samsung in Korea).

Not tested on AVD emulator because there is no contacts on my AVD but I used Eclipse Indigo.

I hope this helps.

Thank you.

package my.android.test; import java.util.*; import android.app.*; import android.content.*; import android.database.*; import android.os.Bundle; import android.provider.*; import android.provider.ContactsContract.CommonDataKinds.*; import android.widget.*; public class GetAllContacts extends Activity { ListView lvItem; String displayName=""; ArrayList<String> contactlist = new ArrayList<String>(); ArrayAdapter<String> itemAdapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lvItem = (ListView)this.findViewById(R.id.lvitems); readContacts(); itemAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,contactlist); lvItem.setAdapter(itemAdapter); } private void readContacts() { ContentResolver cr = getContentResolver(); Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); while (cursor.moveToNext()) { displayName = ""; displayName = cursor .getString(cursor .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); String id = cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts._ID)); String holder = ""; holder = holder + displayName; /* Phone Number and Type */ ArrayList<String> phoneList = new ArrayList<String>(); if (Integer .parseInt(cursor.getString(cursor .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { Cursor pCur = cr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[] { id }, null); while (pCur.moveToNext()) { int phoneType = pCur .getInt(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE)); String pnum = pCur.getString(pCur .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); switch (phoneType) { case ContactsContract.CommonDataKinds.Phone.TYPE_HOME: phoneList.add(", Home, "+pnum); break; case ContactsContract.CommonDataKinds.Phone.TYPE_WORK: phoneList.add(", Work, "+pnum); break; case ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE: phoneList.add(", Mobile, "+pnum); break; case ContactsContract.CommonDataKinds.Phone.TYPE_OTHER: phoneList.add(", Other, "+pnum); break; } } // end of while pCur.close(); } // end of if /* Email */ Cursor emails = cr.query(Email.CONTENT_URI, null, Email.CONTACT_ID + " = " + id, null, null); ArrayList<String> emailList = new ArrayList<String>(); while (emails.moveToNext()) { int emailType = emails .getInt(emails .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE)); String email = emails.getString(emails .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA)); switch (emailType) { case ContactsContract.CommonDataKinds.Email.TYPE_HOME: emailList.add(", Home, "+email); break; case ContactsContract.CommonDataKinds.Email.TYPE_WORK: emailList.add(", Work, "+email); break; case ContactsContract.CommonDataKinds.Email.TYPE_MOBILE: emailList.add(", Mobile, "+email); break; case ContactsContract.CommonDataKinds.Email.TYPE_OTHER: emailList.add(", Other, "+email); break; } } emails.close(); /* Requirements for Abhilash in India */ if(phoneList.size()>=emailList.size()) { for(int i=0;i<phoneList.size();i++) { holder = displayName + phoneList.get(i); try{ holder = holder + emailList.get(i); }catch(Exception ex){ holder = holder + ", , "; } contactlist.add(holder); } }else { for(int i=0;i<emailList.size();i++) { try{ holder = displayName+ phoneList.get(i); }catch(Exception ex){ holder = holder + ", , "; } holder = holder + emailList.get(i); contactlist.add(holder); } } } cursor.close(); } }



Posted by cwisky

댓글을 달아 주세요