Bonsoir,
je suis super débutant pour ne pas dire nul et j'essaye de créer un répertoire téléphonique.Le problème est que je n'arrive à afficher que le dernier élément de la liste.
J'essaye de comprendre mon erreur plutôt que de chercher une solution de contournement.
Merci d'avance pour 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
17
18
19
20
21
22
23
24
25
26
 
public class MainActivity extends Activity {
     ListView vue;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        String[][] repertoire = new String[][]{
                {"Bill Gates", "06 06 06 06 06"},
                {"Niels Bohr", "05 05 05 05 05"},
                {"Alexandre III de Macédoine", "04 04 04 04 04"}};
 
        vue = (ListView) findViewById(R.id.list);
        List <HashMap<String, String>> liste = new ArrayList<>();
        HashMap<String, String> element;
        element = new HashMap<>();
        int j=0;
            for(int i = 0 ;i <repertoire.length ;i++) {element.put("NOM",repertoire[i][j]);element.put("N°",repertoire[i][j+1]);}
        liste.add(element);
        ListAdapter adapter = new SimpleAdapter
                (this,liste,android.R.layout.simple_list_item_1,new String[] {"NOM","N°"},new int[]{android.R.id.text1});
 
         vue.setAdapter(adapter);
    }
}
Code XML : 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
 
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="com.example.m.tal3a.MainActivity">
 
    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_dialog_email" />
 
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:theme="@style/AppTheme.AppBarOverlay">
 
        <Button
            android:id="@+id/button"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:text="-----" />
 
        <ListView
            android:id="@+id/list"
            android:layout_width="200dp"
            android:layout_height="fill_parent" />
    </android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>