Thursday, February 2, 2017

Fetch contact list from device – Android

Just add below patch in your code and you will see result as shown above:
 private 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();  
     }  
   }  

No comments :

Post a Comment