RSS

Fetch contact list from device – Android

26 Aug

Screenshot_2014-08-26-23-00-23


e ArrayList<Map<String, String>> mPeopleList;
private SimpleAdapter mAdapter;
private AutoCompleteTextView mTxtPhoneNo;
 
class FetchPeopleTask extends AsyncTask<String, String, String>{
        @Override
        protected String doInBackground(String... params) {
            Log.d(TAG, "Fetching people task start.");
            PopulatePeopleList();
            return null;
        }
 
        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            Log.d(TAG, "Fetching people task complete.");
        }
    }
 
public void PopulatePeopleList() {
        try {
            mPeopleList.clear();
            Cursor people = getContentResolver().query(
                    ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
            while (people.moveToNext()) {
                String contactName = people.getString(people
                        .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                String contactId = people.getString(people
                        .getColumnIndex(ContactsContract.Contacts._ID));
                String hasPhone = people
                        .getString(people
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
 
                if ((Integer.parseInt(hasPhone) > 0)){
                    // You know have the number so now query it like this
                    Cursor phones = getContentResolver().query(
                            ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                            null,
                            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
                            null, null);
                    while (phones.moveToNext()){
                        //store numbers and display a dialog letting the user select which.
                        String phoneNumber = phones.getString(
                                phones.getColumnIndex(
                                        ContactsContract.CommonDataKinds.Phone.NUMBER));
                        String numberType = phones.getString(phones.getColumnIndex(
                                ContactsContract.CommonDataKinds.Phone.TYPE));
                        Map<String, String> NamePhoneType = new HashMap<String, String>();
                        NamePhoneType.put("Name", contactName);
                        NamePhoneType.put("Phone", phoneNumber);
                        if(numberType.equals("0"))
                            NamePhoneType.put("Type", "Work");
                        else
                            if(numberType.equals("1"))
                                NamePhoneType.put("Type", "Home");
                            else if(numberType.equals("2"))
                                NamePhoneType.put("Type",  "Mobile");
                            else
                                NamePhoneType.put("Type", "Other");
                        //Then add this map to the list.
                        mPeopleList.add(NamePhoneType);
                    }
                    phones.close();
                }
            }
            people.close();
        } catch (NumberFormatException e) {         
            e.printStackTrace();
        }
    }
 
private void bindAutocompletContact(){
        try {
            mAdapter = new SimpleAdapter(this, mPeopleList, R.layout.autocomplete_contact_layout,
                    new String[] { "Name", "Phone", "Type" }, new int[] {
                    R.id.ccontName, R.id.ccontNo, R.id.ccontType });
            mTxtPhoneNo.setAdapter(mAdapter);
            mTxtPhoneNo.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> av, View arg1, int index,
                        long arg3) {
                    Map<String, String> map = (Map<String, String>) av.getItemAtPosition(index);
                    String name  = map.get("Name");
                    String number = map.get("Phone");
                    mTxtPhoneNo.setText(number);
                }
            });
        } catch (Exception e) {         
            e.printStackTrace();
        }
Advertisements
 
Leave a comment

Posted by on August 26, 2014 in Android Source Code

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: