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 :

Recherche dans ListeView


Sujet :

Android

  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    83
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 83
    Par défaut Recherche dans ListeView
    Bonjour à tous,
    Pour une application, j'ai inséré une ListeView, j'ai besoin d'ajouter dans celle-ci une recherche (ou plutôt un filtre), j'ai cherché et ai trouvé des pistes du côté de addTextChangedListener, onTextChanged ... mais je ne comprends pas comment l'adapter sur ma Listeview.

    Voici mes codes :
    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
    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
    public class menu3_Fragment extends Fragment {
        View rootview;
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            rootview = inflater.inflate(R.layout.acidamin, container, false);
     
            ListView listViewAcidAmin = (ListView) rootview.findViewById(R.id.listViewAcidAmin);
     
            ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
     
            HashMap<String, String> map;
     
            map = new HashMap<String, String>();
            map.put("Nom", "Alanine");
            map.put("Code", "A");
            map.put("Abv", "Ala");
            map.put("Nature", "Apolaire, aliphatique");
            map.put("Codon", "GCU, GCC, GCA, GCG");
            map.put("ref","1");
            listItem.add(map);
     
    //Je n'ai mis qu'un listItem.add afin de ne pas surcharger la page
     
     
            SimpleAdapter mSchedule = new SimpleAdapter (rootview.getContext(), listItem, R.layout.affichageitems,
                    new String[] { "Nom"}, new int[] { R.id.type});
            listViewAcidAmin.setAdapter(mSchedule);
            listViewAcidAmin.setOnItemClickListener(new OnItemClickListener() {
                @Override
                @SuppressWarnings("unchecked")
                public void onItemClick(AdapterView<?> a, View v, int position, long id) {
     
                    ListView listViewAcidAmin = (ListView) rootview.findViewById(R.id.listViewAcidAmin);
                    TextView nom = (TextView) rootview.findViewById(R.id.Nom);
                    TextView code = (TextView) rootview.findViewById(R.id.Code);
                    TextView abv = (TextView) rootview.findViewById(R.id.Abv);
                    TextView nat = (TextView) rootview.findViewById(R.id.Nature);
                    TextView codon = (TextView) rootview.findViewById(R.id.Codon);
                    TextView ref = (TextView) rootview.findViewById(R.id.ref);
     
                    HashMap<String, String> map = (HashMap<String, String>) listViewAcidAmin.getItemAtPosition(position);
                    nom.setText(map.get("Nom"));
                    code.setText(map.get("Code"));
                    abv.setText(map.get("Abv"));
                    nat.setText(map.get("Nature"));
                    codon.setText(map.get("Codon"));
                    ref.setText(map.get("ref"));
                }
            });
            return rootview;
        }
    }
    Dans mon xml j'ai la listeview, les EditText dont un pour la recherche.

    Merci par avance pour le coup de main.

  2. #2
    Membre très actif
    Homme Profil pro
    Ingénieur Informatique et Développeur Android
    Inscrit en
    Janvier 2010
    Messages
    384
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Tunisie

    Informations professionnelles :
    Activité : Ingénieur Informatique et Développeur Android

    Informations forums :
    Inscription : Janvier 2010
    Messages : 384
    Par défaut
    Salut,
    l'idée est de créer un adapter personnalisé "extends BaseAdapter implements Filterable" et d'implémenter l'interface Filterable

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    83
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 83
    Par défaut
    bonjour tlili_info et merci pour ta réponse.
    En effet je suis partis dans la mauvaise direction (ou en tout cas ta solution me semble plus simple et je m'en suis sorti!!)

    Voici donc le code avec le filtre :

    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
    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
    public class menu4_Fragment extends Fragment  {
            View rootview;
            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
                rootview = inflater.inflate(R.layout.codon, container, false);
     
                ListView listViewAcidAmin = (ListView) rootview.findViewById(R.id.listViewAcidAmin);
     
                ArrayList<HashMap<String, String>> listItem = new ArrayList<HashMap<String, String>>();
     
                HashMap<String, String> map;
     
                map = new HashMap<String, String>();
                map.put("Nom", "Alanine");
                map.put("Code", "A");
                map.put("Abv", "Ala");
                map.put("Nature", "Apolaire, aliphatique");
                map.put("Codon", "GCU");
                map.put("Codon", "GCC");
                map.put("Codon", "GCG");
                map.put("Codon", "GCA");
                map.put("ref","1");
                listItem.add(map);
     
     
                final SimpleAdapter mSchedule = new SimpleAdapter (rootview.getContext(), listItem, R.layout.affichageitems,
                        new String[] { "Codon"}, new int[] { R.id.type});
                listViewAcidAmin.setAdapter(mSchedule);
                listViewAcidAmin.setTextFilterEnabled(true);
                listViewAcidAmin.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    @SuppressWarnings("unchecked")
                    public void onItemClick(AdapterView<?> a, View v, int position, long id) {
     
                        ListView listViewAcidAmin = (ListView) rootview.findViewById(R.id.listViewAcidAmin);
                        TextView nom = (TextView) rootview.findViewById(R.id.Nom);
                        TextView code = (TextView) rootview.findViewById(R.id.Code);
                        TextView abv = (TextView) rootview.findViewById(R.id.Abv);
                        TextView nat = (TextView) rootview.findViewById(R.id.Nature);
                        TextView codon = (TextView) rootview.findViewById(R.id.Codon);
                        TextView ref = (TextView) rootview.findViewById(R.id.ref);
     
     
                        HashMap<String, String> map = (HashMap<String, String>) listViewAcidAmin.getItemAtPosition(position);
                        nom.setText(map.get("Nom"));
                        code.setText(map.get("Code"));
                        abv.setText(map.get("Abv"));
                        nat.setText(map.get("Nature"));
                        codon.setText(map.get("Codon"));
                        ref.setText(map.get("ref"));
     
                    }
                });
                EditText myFilter = (EditText) rootview.findViewById(R.id.search);
                myFilter.addTextChangedListener(new TextWatcher() {
     
                    public void afterTextChanged(Editable s) {
                    }
     
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
     
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        mSchedule.getFilter().filter(s.toString());
                    }
                });
                return rootview;
            }
        }
    J'ai donc rajouté :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    listViewAcidAmin.setTextFilterEnabled(true);
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
                EditText myFilter = (EditText) rootview.findViewById(R.id.search);
                myFilter.addTextChangedListener(new TextWatcher() {
     
                    public void afterTextChanged(Editable s) {
                    }
     
                    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                    }
     
                    public void onTextChanged(CharSequence s, int start, int before, int count) {
                        mSchedule.getFilter().filter(s.toString());
                    }
    Merci pour le coup de main

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

Discussions similaires

  1. Problème de recherche dans une BD
    Par ledevelopeur dans le forum Bases de données
    Réponses: 5
    Dernier message: 28/04/2004, 09h49
  2. recherche dans un projet
    Par FreshVic dans le forum Eclipse Java
    Réponses: 4
    Dernier message: 26/04/2004, 20h04
  3. [LG]rechercher dans un fichier texte
    Par BadFox dans le forum Langage
    Réponses: 11
    Dernier message: 01/12/2003, 15h57
  4. [BPW]Problème de recherche dans une boîte liste
    Par Alcatîz dans le forum Turbo Pascal
    Réponses: 14
    Dernier message: 05/07/2003, 15h10
  5. recherche dans un document xml via DOM
    Par ndoye_zaff dans le forum APIs
    Réponses: 5
    Dernier message: 11/06/2003, 14h44

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