• Développer une listview personnalisée avec des fichiers xml

    Publié le 16 juin 2009 par Guy (GuyTouch)


    Dans un billet précédent Développer sa Listview personnalisée sous android j’avais expliqué comment créer une listview personnalisée tout en java .Cette fois je vais faire pareil mais avec des fichiers xml.

    Premier fichier celui de ma listview que j’appelle liste1.xml

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
    <?xml version="1.0" encoding="utf-8"?>
    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/container"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
     
        <ListView
            android:id="@android:id/list"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" 
          />     
     
    </FrameLayout>

    Deuxième fichier xml celui de chaque item que j’appelle afficheplanning.xml.

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        >
    <TextView  
    	android:id="@+id/date"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="date"
        />
        <TextView  
    	android:id="@+id/titre"
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="recette"
        />
    </LinearLayout>

    Ensuite dans mon code java à l’endroit où je veux afficher ma listview je rajoute

    ?Download download.txt
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    
    ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
            		HashMap<String, String> map = new HashMap<String, String>();
            		map.put("date", "01/01/2009");
            		map.put("titre", "Jean");
            		mylist.add(map);
            		map = new HashMap<String, String>();
            		map.put("date", "29/12/2009");
            		map.put("titre", "David");
            		mylist.add(map);
     
            		//j'appelle le fichier de ma liste view liste1.xml
            		setContentView(R.layout.liste1);
         	                mRecetteList = (ListView) findViewById(android.R.id.list);
                   //je crée un objet simpleAdapter avec le fichier xml des item afficheplanning.xml
         	      SimpleAdapter mSchedule = new SimpleAdapter(this, mylist, R.layout.afficheplanning,
         	             new String[] {"date", "titre"}, new int[] {R.id.date, R.id. titre});
     
     
         	        //setListAdapter(new SpeechListAdapter(this));
         	       mRecetteList.setAdapter(mSchedule);
            		return true;

    résultat
    listwiew2 180x300 Développer une listview personnalisée avec des fichiers xml Android France
    aprés il ne reste plus avec les feuilles de style avec ce billet Customiser sa listview avec des feuilles de style de modifier l’apparence de la listview

    Il est temps de mettre à profit les tutoriels d’Android France:
    Passez à la vitesse supérieure et investissez quelques dizaines d’euros pour acquérir les connaissances qui vous feront gagner de l’argent avec vos applications rendez-vous sur notre boutique Android-france pour ces formations en vidéo


    Guy

    Co-fondateur du site Android france, senior lead developper, passionné de bière et de cigare cubain

    Twitter Google+ 

  • Les Brèves