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 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
| import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class GuideActivity extends Activity {
private ListView maListViewPerso;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//Récupération de la listview créée dans le fichier main.xml
maListViewPerso = (ListView) findViewById(R.id.listviewperso);
//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 élément titre que l'on récupérera dans le textView titre créé dans le fichier affichageitem.xml
map.put("titre", "Frensh");
//on insère un élément description que l'on récupérera dans le textView description créé dans le fichier affichageitem.xml
map.put("description", "");
//on insère la référence à l'image (convertit en String car normalement c'est un int) que l'on récupérera dans l'imageView créé dans le fichier affichageitem.xml
map.put("img", String.valueOf(R.drawable.fr));
//enfin on ajoute cette hashMap dans la arrayList
listItem.add(map);
map = new HashMap<String, String>();
map.put("titre", "English");
map.put("description", "");
map.put("img", String.valueOf(R.drawable.eng));
listItem.add(map);
//Création d'un SimpleAdapter qui se chargera de mettre les items présent dans notre list (listItem) dans la vue affichageitem
SimpleAdapter mSchedule = new SimpleAdapter (this.getBaseContext(), listItem, R.layout.affichageitem,
new String[] {"img", "titre", "description"}, new int[] {R.id.img, R.id.titre, R.id.description});
//On attribut à notre listView l'adapter que l'on vient de créer
maListViewPerso.setAdapter(mSchedule);
//Enfin on met un écouteur d'évènement sur notre listView
maListViewPerso.setOnItemClickListener(new OnItemClickListener() {
@SuppressWarnings("unchecked")
public void onItemClick1(AdapterView<?> a, View v, int position, long id) {
//on récupère la HashMap contenant les infos de notre item (titre, description, img)
HashMap<String, String> map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position);
//on créer une boite de dialogue
AlertDialog.Builder adb = new AlertDialog.Builder(GuideActivity.this);
//on attribut un titre à notre boite de dialogue
adb.setTitle("Sélection Item");
//on insère un message à notre boite de dialogue, et ici on affiche le titre de l'item cliqué
adb.setMessage("Votre choix : "+map.get("titre"));
//on indique que l'on veut le bouton ok à notre boite de dialogue
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface adb, int which) {
// TODO Auto-generated method stub
Intent intent = new Intent(GuideActivity.this,HelloGoogleMapActivity.class);
startActivity(intent);
}
//public void onCancel(DialogInterface dialog) {}
});
//on affiche la boite de dialogue
adb.show();
}
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
// TODO Auto-generated method stub
//on récupère la HashMap contenant les infos de notre item (titre, description, img)
HashMap<String, String> map = (HashMap<String, String>) maListViewPerso.getItemAtPosition(position);
//on créé une boite de dialogue
AlertDialog.Builder adb = new AlertDialog.Builder(GuideActivity.this);
//on attribue un titre à notre boite de dialogue
adb.setTitle("Sélection Item");
//on insère un message à notre boite de dialogue, et ici on affiche le titre de l'item cliqué
adb.setMessage("Votre choix : "+map.get("titre"));
//on indique que l'on veut le bouton ok à notre boite de dialogue
adb.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface adb, int which) {
// TODO Auto-generated method stub
/*+++++++++++probleme+++++*/
Intent intent = new Intent(GuideActivity.this,HelloGoogleMapActivity.class);
startActivity(intent);
}
//public void onCancel(DialogInterface dialog) {}
});
//on affiche la boite de dialogue
adb.show();
}
});
}
} |
Partager