Comment récupérer les éléments cochés sur un recyclerView dans un fragment.
Bonjour,

j'ai un fragment qui affiche tous les contacts dans un recyclerView avec un checkbox, je veux maintenant récupérer dans une liste tous les contacts cochés par l'utilisateur avec le checkbox.

Voici le code de mon fragment :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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) {
 
    }
}
Pour mon adapter :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
public class ContactRecycleAdapter extends RecyclerView.Adapter<ContactRecycleAdapter.ViewHolder> {
 
    private List<Contact> listItems;
    Context context;
 
 
    public ContactRecycleAdapter(List<Contact> listItems, Context context) {
        this.listItems = listItems;
        this.context = context;
    }
 
    @Override
    public ContactRecycleAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.contact_vue_recycle,parent,false);
        return new ViewHolder(view);
    }
 
    @Override
    public void onBindViewHolder(final ContactRecycleAdapter.ViewHolder holder, int position) {
        final Contact item = listItems.get(position);
 
        holder.tv_nom.setText(item.getNom());
        holder.tv_telephone.setText(item.getTelephone());
 
//        holder.layout.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//
//            }
//        });
    }
 
    @Override
    public int getItemCount() {
        return listItems.size();
    }
 
    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
 
        private TextView tv_nom, tv_telephone;
        public LinearLayout layout;
        private CheckBox cb_select_contact ;
        public ViewHolder(View itemView) {
            super(itemView);
 
            tv_nom =  itemView.findViewById(R.id.tv_nom_recycle);
            tv_telephone =  itemView.findViewById(R.id.tv_tel_recycle);
            layout =  itemView.findViewById(R.id.linear_layout);
 
            cb_select_contact =  itemView.findViewById(R.id.cb_select_contact);
 
        }
    }
 
}
Merci d'avance!!!!