BITWIS3

Untitled

Feb 27th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1.  //POJO
  2.  
  3.  
  4. public class Contact {
  5.     public String name = "";
  6.     public String number = "";
  7.     public String id = "";
  8.  
  9.  
  10.     public Contact(String name, String phoneNumber){
  11.         this.name = name;
  12.         this.number = phoneNumber;
  13.     }
  14.  
  15.  
  16.     public void setName(String name) {
  17.         this.name = name;
  18.     }
  19.  
  20.     public void setNumber(String number) {
  21.         this.number = number;
  22.     }
  23.  
  24.     public void setId(String id) {
  25.         this.id = id;
  26.     }
  27.  
  28.     public String getName() {
  29.         return name;
  30.     }
  31.  
  32.     public String getNumber() {
  33.         return number;
  34.     }
  35.  
  36.     public String getId() {
  37.         return id;
  38.     }
  39.  
  40.  
  41. }
  42.  
  43.  
  44. //method to retrieve all contacts and put into arraylist
  45.  
  46. public static ArrayList<Contact> getContactsList(Context context) {
  47.         ArrayList<Contact> contacts=new ArrayList<>();
  48.         Cursor phones = context.getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  49.                 null, null, null, null);
  50.         while (phones.moveToNext())
  51.         {
  52.             String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  53.             String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
  54.             contacts.add(new Contact(name,phoneNumber));
  55.         }
  56.         phones.close();
  57.         Collections.sort(contacts, new Comparator<Contact>() {
  58.             public int compare(Contact v1, Contact v2) {
  59.                 return v1.getName().compareTo(v2.getName());
  60.             }
  61.         });
  62.         return contacts;
  63.     }
Advertisement
Add Comment
Please, Sign In to add comment