IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Créer un adaptateur personnalisé avec BaseAdapter


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Candidat au Club
    Homme Profil pro
    amateur
    Inscrit en
    Août 2020
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : amateur
    Secteur : Service public

    Informations forums :
    Inscription : Août 2020
    Messages : 3
    Par défaut Créer un adaptateur personnalisé avec BaseAdapter
    Bonjour a tous, je debute sous android et je cherche à créer une liste de vues personnaliser, mais j'ai un peu de mal à comprendre comment tout cela fonctionne et la seul chose que je parviens à obtenir c'est une page blanche.

    Voici mon code:

    Tout d'abord le xml:
    le fichier activity.xml:

    Code XML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vuePrincipal">
     
        <ListView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/listView"/>
     
    </LinearLayout>


    le fichier vue.xml:

    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
    <?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"
        android:id="@+id/vueList">
     
        <TextView
            android:id="@+id/nomPrenom"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
     
        <TextView
            android:id="@+id/numTel"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
     
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:contentDescription="image"/>
    </LinearLayout>

    ensuite le code 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
    package fr.adaptateurperso;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.widget.ListView;
     
    public class AdaptateurPerso extends Activity
    {
        protected void onCreat(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            ListView list = (ListView) findViewById(R.id.listView);
     
            int vue = R.layout.activity;
            setContentView(vue);
     
            RepertoireAdapter adapter = new RepertoireAdapter(this);
            list.setAdapter(adapter);
        }
    }
    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
    package fr.adaptateurperso;
     
    import android.content.Context;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
     
    public class RepertoireAdapter extends BaseAdapter
    {
        private int[] tab_images_pour_la_liste = {
                R.drawable.maman, R.drawable.mami,
                R.drawable.manif, R.drawable.papi, R.drawable.phare};
     
        private String[] valeurNom = new String[] { "maman",
                "mamie", "manif", "papi", "phare"},
                         valeurNum = new String[]{"111111", "2222222", "3333333", "4444444", "55555555"};
     
        private Context contexte;
     
        public RepertoireAdapter(Context contexte)
        {
            super();
            contexte = contexte;
        }
     
        @Override
        public int getCount()
        {
            return tab_images_pour_la_liste.length;
        }
     
        @Override
        public Object getItem(int position)
        {
            return tab_images_pour_la_liste[position];
        }
     
        @Override
        public long getItemId(int position)
        {
            return position;
        }
     
     
        LayoutInflater inflater;
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder = null;
     
            if(convertView == null)
            {
                convertView = inflater.inflate(R.layout.vue, null);
     
                holder = new ViewHolder();
                holder.nom = (TextView) convertView.findViewById(R.id.nomPrenom);
                holder.num = (TextView) convertView.findViewById((R.id.numTel));
                holder.photo = (ImageView) convertView.findViewById(R.id.imageView);
     
                convertView.setTag(holder);
            }
            else
            {
                holder.nom.setText(valeurNom[position]);
                holder.num.setText(valeurNum[position]);
                holder.photo.setImageResource(tab_images_pour_la_liste[position]);
            }
     
            return convertView;
        }
     
        static class ViewHolder
        {
            public TextView nom,
                            num;
            public ImageView photo;
        }
    }
    Voila, un grand merci à celui ou celle qui saura m'explique comment créer un adaptateur personnalisé correctement.

  2. #2
    Candidat au Club
    Homme Profil pro
    amateur
    Inscrit en
    Août 2020
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : amateur
    Secteur : Service public

    Informations forums :
    Inscription : Août 2020
    Messages : 3
    Par défaut
    Bonjour, je reviens vers vous après avoir corrigé quelque erreurs dans le onCreate()

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    {
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            int vue = R.layout.activity;
            setContentView(vue);
     
            ListView list = (ListView) findViewById(R.id.listView);
     
            RepertoireAdapter adapter = new RepertoireAdapter(this);
            list.setAdapter(adapter);
        }
    }
    Apres correction, j'obtiens cette erreur:

    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.LayoutInflater.inflate(int, android.view.ViewGroup)' on a null object reference
    at fr.adaptateurperso.RepertoireAdapter.getView(RepertoireAdapter.java:62)



    je ne comprend pas pourquoi je me retrouve avec un LayoutInflater à null??

  3. #3
    Candidat au Club
    Homme Profil pro
    amateur
    Inscrit en
    Août 2020
    Messages
    3
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 75
    Localisation : France, Allier (Auvergne)

    Informations professionnelles :
    Activité : amateur
    Secteur : Service public

    Informations forums :
    Inscription : Août 2020
    Messages : 3
    Par défaut
    Bien j'ai finalement trouve comment faire ce que je voulais. Ce monologue aura donc été profitable

    Voici la solution au cas ou il y en d'autre qui galèrent comme moi:

    activity_main.xml:

    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
    37
    38
    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/vuePrincipal">
     
        <TextView
            android:id="@+id/nomPrenom"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="dfdgdfgdfhghf"/>
     
        <TextView
            android:id="@+id/numTel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@+id/nomPrenom"
            android:text="113543643648"/>
     
        <ImageView
            android:id="@+id/imageView"
            android:layout_width="60dp"
            android:layout_height="60dp"
            android:layout_marginLeft="6dp"
            android:layout_marginRight="6dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:contentDescription="image"
            android:src="@drawable/sourir"
            android:layout_alignParentRight="true"/>
     
        <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listView"
            android:background="@color/colorAccent"/>
    </RelativeLayout>

    la class MainActivity:

    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
    package fr.adaptateurperso;
     
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ListView;
     
    import java.util.ArrayList;
     
     
    public class MainActivity extends Activity
    {
        private ArrayList<Contact> listContact;
        private int[] image = {
                R.drawable.sourir, R.drawable.heureux,
                R.drawable.clin_oeil};
        private String[] valeurNom = new String[] { "maman",
                "mamie", "papi",},
                valeurNum = new String[]{"111111", "2222222", "3333333"};
     
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            int vue = R.layout.activity_main;
            setContentView(vue);
     
            ListView list = (ListView) findViewById(R.id.listView);
     
            listContact = new ArrayList<Contact>();
            for(int i=0; i<3; i++)
            {
                listContact.add(new Contact(valeurNom[i],valeurNum[i], image[i]));
            }
     
            RepertoireAdapter adapter = new RepertoireAdapter(this, listContact);
            list.setAdapter(adapter);
        }
    }
    la class RepertoirAdapter:

    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
    package fr.adaptateurperso;
     
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;
     
    import java.util.ArrayList;
     
    public class RepertoireAdapter extends BaseAdapter
    {
        private Context contexte;
        private ArrayList<Contact> contact;
     
        public RepertoireAdapter(Context contexte, ArrayList<Contact> contact)
        {
            super();
            this.contexte = contexte;
            this.contact = contact;
        }
     
        @Override
        public int getCount()
        {
            Log.i("getCount() => ", String.valueOf(contact.size()));
            return contact.size();
        }
     
        @Override
        public Object getItem(int position)
        {
            Log.i("getItem() => ", String.valueOf(contact.get(position)));
            return contact.get(position);
        }
     
        @Override
        public long getItemId(int position)
        {
            Log.i("getItemId => ", String.valueOf(position));
            return position;
        }
     
     
        LayoutInflater inflater;
        ViewHolder holder = null;
     
        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            ViewHolder holder = null;
     
            if(convertView == null)
            {
                convertView = LayoutInflater.from(contexte).inflate(R.layout.activity_main, null);
     
                holder = new ViewHolder();
                holder.nom = (TextView) convertView.findViewById(R.id.nomPrenom);
                holder.num = (TextView) convertView.findViewById((R.id.numTel));
                holder.photo = (ImageView) convertView.findViewById(R.id.imageView);
     
                convertView.setTag(holder);
            }
            else
            {
                holder = (ViewHolder) convertView.getTag();
            }
     
            Contact c = (Contact) getItem(position);
            if(c != null)
            {
                holder.nom.setText(c.getNom());
                holder.num.setText(c.getNum());
                holder.photo.setImageResource(c.getImage());
            }
     
            return convertView;
     
        }
     
        static class ViewHolder
        {
            public TextView nom,
                            num;
            public ImageView photo;
        }
    }

    et pour finir la class Contact:

    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
    package fr.adaptateurperso;
     
    public class Contact
    {
        private String nom,
                       num;
        private int image;
     
        public Contact(String nom, String num, int image)
        {
            this.nom = nom;
            this.num = num;
            this.image = image;
        }
     
        public String getNom()
        {
            return nom;
        }
     
        public String getNum()
        {
            return num;
        }
     
        public int getImage()
        {
            return image;
        }
    }

    Voila comme ça marche!!!!!
    Merci à moi!!!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Réponses: 10
    Dernier message: 24/07/2020, 17h53
  2. [Framework] Créer un parseur personnalisé avec un bean interne
    Par Baptiste Wicht dans le forum Spring
    Réponses: 8
    Dernier message: 23/02/2010, 23h44
  3. Créer ma listbox personnalisé avec un Tpanel ? TScrollBox ?
    Par Coussati dans le forum Composants VCL
    Réponses: 14
    Dernier message: 04/01/2009, 21h17
  4. Comment créer un menu personnalisé avec Access2007
    Par marionAccess dans le forum Access
    Réponses: 6
    Dernier message: 24/01/2007, 17h29

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo