j'ai une ListView composée d' items. chaque item contient des textView , une image et un bouton. Je voulais associer un évenement lors d'un clic sur un bouton pour ouvrir une boite d'alerte.
voici le code :
itemfilm.xml
FilmActivity.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
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 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:background="@drawable/border1" android:id="@+id/Layout2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:orientation="horizontal"> <ImageView android:id="@+id/img_film" android:layout_width="80dp" android:layout_height="100dp" android:layout_marginTop="0dip" android:layout_marginLeft="10dip" /> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/Layout3" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginTop="0dip" android:layout_marginLeft="10dip" > <TextView android:id="@+id/nom_film" android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:layout_marginTop="10dip" android:textSize="14sp" android:textStyle="bold" android:textColor="@android:color/white" /> <TextView android:id="@+id/genre_film" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0dp" android:layout_marginTop="0dip" android:textSize="12sp" android:textColor="@android:color/white" /> <TextView android:id="@+id/duree_film" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="0dp" android:layout_marginTop="0dip" android:textSize="12sp" android:textColor="@android:color/white" /> <TextView android:id="@+id/lieu_film" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="0dp" android:layout_marginTop="0dip" android:textSize="12sp" android:textColor="@android:color/white" /> <TextView android:id="@+id/horaires_film" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginLeft="0dp" android:layout_marginTop="0dip" android:textSize="12sp" android:textColor="@android:color/white" /> <Button android:id="@+id/btn_synopsis" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" android:layout_marginTop="0dp" android:textStyle="italic" android:textSize="12sp" android:textColor="#800000" /> </LinearLayout> </LinearLayout>
Lors du clic sur le bouton "Synopsis", il y aura affichage d'une boite de dialogue qui affichera le synopsis du film.
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
105 public class FilmsActivity extends Activity /*implements OnClickListener*/{ private Button bt_synopsys; private ListView list_films; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.films); list_films= (ListView) findViewById(R.id.listview_films); FillList(); // bt_synopsys=(Button)findViewById(R.id.btn_synopsis); //bt_synopsys.setOnClickListener(this); } private void FillList() { //Création de la ArrayList qui nous permettra de remplire la listView ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>(); //On déclare la HashMap qui contiendra les informations pour un item HashMap<String, String> map; //Création d'une HashMap pour insérer les informations du premier item de notre listView map = new HashMap<String, String>(); //on insère un element image que l'on récupérera dans le textView img_film créé dans le fichier itemfilm.xml map.put("img_film",String.valueOf(R.drawable.image_film)); map.put("nom_film", "Paul"); map.put("genre_film", "Comédie, Romance"); map.put("duree_film", "92min"); map.put("lieu_film", "Cinéma El Manar1"); map.put("horaires_film", "11h 16h 20h"); map.put("btn_synopsis","Synopsis"); //enfin on ajoute cette hashMap dans la arrayList listItem.add(map); //Création d'une HashMap pour insérer les informations du deuxième item de notre listView map = new HashMap<String, String>(); //on insère un element image que l'on récupérera dans le textView img_film créé dans le fichier itemfilm.xml map.put("img_film",String.valueOf(R.drawable.image_film)); map.put("nom_film", "Un jour mon père viendra"); map.put("genre_film", "Comédie, Romance"); map.put("duree_film", "92min"); map.put("lieu_film", "Cinéma El Manar1"); map.put("horaires_film", "11h 16h 20h"); map.put("btn_synopsis","Synopsis"); //enfin on ajoute cette hashMap dans la arrayList listItem.add(map); //Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue itemfilm.xml SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.itemfilm, new String[] {"img_film", "nom_film", "genre_film","duree_film","lieu_film","horaires_film","btn_synopsis"}, new int[] {R.id.img_film, R.id.nom_film, R.id.genre_film, R.id.duree_film, R.id.lieu_film,R.id.horaires_film,R.id.btn_synopsis}); //On attribut à notre listView l'adapter que l'on vient de créer list_films.setAdapter(mSchedule); } /*public void onClick(View v) { //On instancie notre layout en tant que View LayoutInflater factory = LayoutInflater.from(this); final View alertDialogView = factory.inflate(R.layout.synopsys, null); //Création de l'AlertDialog AlertDialog.Builder adb = new AlertDialog.Builder(this); //On affecte la vue personnalisé que l'on a crée à notre AlertDialog adb.setView(alertDialogView); //On donne un titre à l'AlertDialog adb.setTitle("Synopsys"); //On modifie l'icône de l'AlertDialog pour le fun ;) //adb.setIcon(android.R.drawable.ic_dialog_info); adb.setNeutralButton("Fermer",new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { //Lorsque l'on cliquera sur annuler on quittera l'application dialog.cancel(); } }); adb.show(); }*/ }
synopsis.xml
Merci de votre aide.
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 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="15dip"> <TextView android:id="@+id/Text_synop" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="La vie de Paul bascule le jour où sa femme Sarah disparait subitement. Après une année de recherches infructueuses, Paul est un homme brisé, rongé par le doute et la culpabilité. Sa dernière chance est peut être de tout reprendre à zéro : déménager avec ses 2 enfants à Saint-Malo, la ville où il a grandit. Mais des rencontres inattendues vont donner à ce nouveau départ une tournure qu'il n'imaginait pas." android:paddingBottom="10dip" /> </RelativeLayout>
Partager