Bonjour,
Je dois afficher une liste en utisant RecyclerView.
Voici le code :
list_place.xml :
detail_place.xml :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ListPlaceActivity"> <android.support.v7.widget.RecyclerView android:id="@+id/list" android:layout_width="match_parent" android:layout_height="match_parent" /> </RelativeLayout>
ListPlaceActivity.java :
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="15dp" android:orientation="vertical"> <TextView android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_marginBottom="15dp" android:textAppearance="?android:attr/textAppearanceLarge" tools:text="Personnage" ></TextView> <TextView android:id="@+id/description" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:lines="1" android:ellipsize="end" android:textAppearance="?android:attr/textAppearanceSmall" tools:text="Description" ></TextView> </LinearLayout>
PlaceAdapter.java avec la sous-classe PlaceViewHolder :
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 package net.ms2i.actech; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import net.ms2i.actech.addon.PlaceAdapter; public class ListPlaceActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_place); final RecyclerView rv = findViewById(R.id.list); rv.setLayoutManager(new LinearLayoutManager(this)); rv.setAdapter(new PlaceAdapter()); } }
Quand j'exécute l'application, j'obtiens toujours le premier élément de la liste "listPair" soit "AAAA" pour "name" et "aaaa" pour "description".
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 package net.ms2i.actech.addon; import android.app.AlertDialog; import android.support.v7.widget.RecyclerView; import android.util.Pair; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.TextView; import net.ms2i.actech.R; import java.util.Arrays; import java.util.List; public class PlaceAdapter extends RecyclerView.Adapter<PlaceAdapter.PlaceViewHolder> { private final List<Pair<String, String>> listPair = Arrays.asList( Pair.create("AAAA", "aaaa"), Pair.create("BBBB", "bbbb"), Pair.create("CCCC", "cccc"), Pair.create("DDDD", "dddd"), Pair.create("EEEE", "eeee"), Pair.create("FFFF", "ffff"), Pair.create("GGGG", "gggg"), Pair.create("HHHH", "hhhh"), Pair.create("IIII", "iiii"), Pair.create("JJJJ", "jjjj") ); @Override public int getItemCount() { return listPair.size(); } @Override public PlaceViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { LayoutInflater inflater = LayoutInflater.from(parent.getContext()); View view = inflater.inflate(R.layout.detail_place, parent, false); return new PlaceViewHolder(view); } @Override public void onBindViewHolder(PlaceViewHolder holder, int position) { Pair<String, String> pair = listPair.get(position); holder.display(pair); } public class PlaceViewHolder extends RecyclerView.ViewHolder { private final TextView name; private final TextView description; private Pair<String, String> currentPair; public PlaceViewHolder(final View itemView) { super(itemView); name = itemView.findViewById(R.id.name); description = itemView.findViewById(R.id.description); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new AlertDialog.Builder(itemView.getContext()) .setTitle(currentPair.first) .setMessage(currentPair.second) .show(); } }); } public void display(Pair<String, String> pair) { currentPair = pair; name.setText(pair.first); description.setText(pair.second); } } }
Où donc se trouve l'erreur qui empêche l'affichage de tous les éléments de la liste ?
Merci pour votre aide.
A+
Partager