Salut à tous,
J'ai un projet d'application "Todo" ou je liste des tâches dans une ListView. Chaque item de ma ListView comporte un certain nombre de composant dont un Switch. Le problème est que, quand je clique sur le switch d'une tâche, la tâche est bien supprimé (comportement que je souhaite) mais la tâche suivante est coché également (sans être supprimé).
Ma ListView est implémenté avec un Adapter dont voici le code :
et voici le code de mon item tache :
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 public class Adapter extends BaseAdapter { private List<Tache> lesTaches; private Context context; private LayoutInflater inflater; public Adapter(Context context, List<Tache> list) { this.context = context; lesTaches = list; inflater = LayoutInflater.from(context); } @Override public int getCount() { return lesTaches.size(); } @Override public Object getItem(int position) { return lesTaches.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(final int position, View convertView, ViewGroup parent) { View view; if(convertView == null) { view = (View) inflater.inflate(R.layout.tache_item, parent, false); } else { view = (View) convertView; } TextView intitule = (TextView) view.findViewById(R.id.intitule); intitule.setText(lesTaches.get(position).getIntitule()); final Switch switch1 = (Switch) view.findViewById(R.id.switch1); switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { if(buttonView.isPressed()) { if(isChecked) { remove(lesTaches.get(position)); } } } }); return view; } public void remove(Tache toDel) { lesTaches.remove(toDel); notifyDataSetChanged(); } public void addItem(Tache toAdd) { lesTaches.add(toAdd); notifyDataSetChanged(); } }
Finalement, voici un screen de mon application montrant le problème. Le screen a été pris avoir cliquer sur le switch de la première tâche (elle n'apparait donc pas) mais la tache suivante est coché (sans être supprimé).
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 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"> <Switch android:id="@+id/switch1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="false" android:layout_marginHorizontal="5dp" android:focusable="false" /> <TextView android:id="@+id/intitule" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toEndOf="@+id/switch1" android:layout_toRightOf="@+id/switch1" /> <ImageView android:id="@+id/icon_info" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_weight="1" android:layout_marginHorizontal="5dp" app:srcCompat="@drawable/ic_info_black" /> </RelativeLayout>
Je vous remercie pour votre lecture et attends vos réponse avec impatience.
Mathis.
Partager