1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
|
public class ContactLocalFragment extends Fragment implements View.OnClickListener {
private RecyclerView.Adapter adapter;
private ArrayList<Contact> listItems;
private ArrayList<Contact> contactList_select;
private RecyclerView recyclerView;
private FloatingActionButton fabServer;
public ContactLocalFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view= inflater.inflate(R.layout.fragment_contact_local, container, false);
contactList_select = new ArrayList<>();
recyclerView = view.findViewById(R.id.rv_contact);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
fabServer = view.findViewById(R.id.btn_flot_sync_server);
fabServer.setOnClickListener(this);
listItems = new ArrayList<>();
listItems = getContacts();
adapter = new ContactRecycleAdapter(listItems, getActivity());
recyclerView.setAdapter(adapter);
return view;
}
public void prepareSelection(View view, int position) {
if (((CheckBox) view).isChecked()) {
contactList_select.add(listItems.get(position));
} else {
contactList_select.remove(listItems.get(position));
}
}
private ArrayList<Contact> getContacts() {
ArrayList<Contact> contacts = new ArrayList<>();
// Get the Cursor of all the contacts
Cursor cursor = null;
try {
cursor = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
int contactIdIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID);
int nameIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
int phoneNumberIdx = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
cursor.moveToFirst();
do {
Contact contact = new Contact();
String idContact = cursor.getString(contactIdIdx);
String name = cursor.getString(nameIdx);
String phoneNumber = cursor.getString(phoneNumberIdx);
//String mail = cursor.getString(mailIdx);
String email = null;
Cursor emailCur = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{idContact}, null);
while (emailCur.moveToNext()) {
email = emailCur.getString(emailCur.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
// Log.e("Email", name + " " + email);
}
emailCur.close();
//...
contact.setId(idContact);
contact.setNom(name);
contact.setTelephone(phoneNumber);
contact.setEmail(email);
contacts.add(contact);
//Log.i("ContactListActivity", "=============== ================ ================= remonter : " + contact.toString());
} while (cursor.moveToNext());
} catch (Exception e) {
e.printStackTrace();
} finally {
if (cursor != null) {
cursor.close();
}
}
// Close the curosor
cursor.close();
return contacts;
}
@Override
public void onClick(View view) {
}
} |
Partager