Les ListView sous Android

Dans Android,  les ListView vous permettent d’arranger  les informations dans une liste déroulante verticale.
Dans cet article, nous allons vous montrer comment mettre en place votre ListView en utilisant SimpleAdapter:

Partie XML

main.xml

Nous allons adopter cette structure dans notre fichier main.xml



<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ListView android:id="@+id/list" android:layout_width="match_parent"
        android:layout_height="wrap_content" android:layout_weight="1.0" />
</LinearLayout>


item_template.xml

Maintenant créons un nouveau fichier XML que l'on va appeler item_template.xml, permettant de créer la vue qui affichera l'item dans la ListView.


<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="horizontal"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
        >
    <TextView android:id="@+id/tmp_item_marque"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:textSize="16px"
              android:textStyle="bold"
            />

    <TextView android:id="@+id/tmp_item_system"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
            />
</LinearLayout>


Partie JAVA



@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] marque = new String[]{"HTC","Sumsung","Nokia","IPhone","IPad","Nexus"};
        String[] system = new String[]{"Android","Android","Windows Mobile","IOS","IOS","Android"};
       
        ListView listItem = (ListView)findViewById(R.id.list);
        List<Map<String, String>> values = new ArrayList<Map<String, String>>();
        Map<String, String> map = null;
        for (int i=0;i<6;i++) {
            map = new HashMap<String,String>();
            map.put("item_marque", marque[i]);
            map.put("item_system", system[i]);
            values.add(map);
        }
        String[] from = new String[]{"item_marque","item_system"};
        int[] to = new int[]{R.id.tmp_item_marque,R.id.tmp_item_system};
        //Initialisation
        SimpleAdapter adapter = new SimpleAdapter(ListItemActivity.this,values,
                R.layout.item_template, from, to);

        listItem.setAdapter(adapter);

//Enfin on met un écouteur d'évènement sur notre listView
        listItem.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            @SuppressWarnings("unchecked")
            public void onItemClick(AdapterView<?> a, View v, int position, long id) {
                //on récupère la HashMap contenant les infos de notre item (marque, sytem)
                HashMap<String, String> map = (HashMap<String, String>) listItem.getItemAtPosition(position);
                //on créé une boite de dialogue
                AlertDialog.Builder adb = new AlertDialog.Builder(NotificationActivity.this);
                //on attribue un titre à notre boite de dialogue
                adb.setTitle("Sélection Marque");
                //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("item_marque"));
                //on indique que l'on veut le bouton ok à notre boite de dialogue
                adb.setPositiveButton("Ok", null);
                //on affiche la boite de dialogue
                adb.show();
            }

        });
    }






Donc il suffit maintenant de lancer votre application et tester le résultat et Merci ;)

Comments

Popular posts from this blog

Exportation et Importation de vos sites sous IIS7 ou IIS8