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 :

Ajouter image avec un ArrayAdapter


Sujet :

Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 61
    Par défaut Ajouter image avec un ArrayAdapter
    Bonjour,

    J'ai un AutoCompleteTextView qui me propose des suggestions lorsque j'écris dans celui-ci.
    J'ai un ArrayAdapter custom dans le quel je fais une recherche pour les suggestions.

    J'aimerais maintenant qu'il y ai une image à coté de chaque suggestions dans ma liste de suggestion.

    Voici mon ArrayAdapter custom:

    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
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    package com.ac3distribution.immofacile.parser;
     
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.List;
     
    import org.json.JSONArray;
    import org.json.JSONException;
    import org.json.JSONObject;
     
    import com.ac3distribution.immofacile.activity.tablet.WebTabletActivity;
    import com.ac3distribution.immofacile.tasks.SearchTask;
     
    import android.app.Activity;
    import android.widget.ArrayAdapter;
    import android.widget.Filter;
     
    public class SuggestionAdapter extends ArrayAdapter<String> {
     
    	HashMap<Integer,String> url = new HashMap<Integer,String>();
    	WebTabletActivity activ;
    	String result;
        protected static final String TAG = "SuggestionAdapter";
        private List<String> suggestions;
     
        public SuggestionAdapter(Activity context, String nameFilter, WebTabletActivity activ, String result) {
            super(context, android.R.layout.simple_dropdown_item_1line);
            suggestions = new ArrayList<String>();
            this.activ = activ;
            this.result = result;
        }
     
        @Override
        public int getCount() {
            return suggestions.size();
        }
     
        @Override
        public String getItem(int index) {
            return suggestions.get(index);
        }
     
     
     
     
        @Override
        public Filter getFilter() {
            Filter myFilter = new Filter() {
                @Override
                protected FilterResults performFiltering(CharSequence constraint) {
                    FilterResults filterResults = new FilterResults();
                    List<Integer> listType = new ArrayList<Integer>();
                    List<String> listTri = new ArrayList<String>();
     
     
                    if (constraint != null) {
     
     
                        suggestions.clear();
     
     
                		//Effectue la recherche, les résultats sont dans la variable result
                		new SearchTask(activ, constraint.toString(), activ).execute();
     
                		try {
     
                			System.out.println(activ.getResult());
                			//Objet Json avec le resultat
                	        JSONObject json = new JSONObject(activ.getResult());
     
                	        //On récupère les clés
                	        JSONArray str = json.names();
     
                	        //Taille pour la boucle
                	        int taille = 0;
                	        if(str.length() > 9)
                	        	taille = 9;
                	        else{taille = str.length();}
     
                	        	for(int i = 0; i < taille ; i++){
     
     
                	        		//Récupère l'objet JSON en fonction de la clé
                					JSONObject jo = json.getJSONObject(str.getString(i));
     
                	        		if(jo.getString("type_result").equals("admin")){
                	        			String adminNom = "";
                	        			String adminPrenom = "";
     
                	        			try{
                	        				adminNom = jo.getString("admin_lastname");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				adminPrenom = jo.getString("admin_firstname");
                	        			}catch (JSONException e) {}
     
                	        			String suggest = "0"+adminNom + " " + adminPrenom;
                	        			suggestions.add(suggest);
                	        			listType.add(0);
     
                	        		}
     
     
                	        		else if(jo.getString("type_result").equals("products")){
                	        			String typeLoye = "";
                	        			String etat = "";
                	        			String prix = "";
                	        			String ville = "";
                	        			String mandat = "";
                	        			String vendeur = "";
                	        			String productsModel = "";
                	        			String adminNom = "";
     
                	        			try{
                	        				typeLoye = jo.getString("C_27");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				etat = jo.getString("C_28");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				prix = jo.getString("products_price");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				ville = jo.getString("C_65");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				mandat = jo.getString("C_121");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				vendeur = jo.getString("seller");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				productsModel = jo.getString("products_model");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				adminNom = jo.getString("admin_name");
                	        			}catch (JSONException e) {}
     
                	        			String suggest = "3"+typeLoye + " " + etat + " " + prix + "\n" + ville + " " + mandat + " " + vendeur + " \n" + productsModel + " " + adminNom;
                	        			suggestions.add(suggest);
                	        			listType.add(3);
     
                	        		}
     
     
                	        		else if(jo.getString("type_result").equals("customers")){
     
                	        			String clientNom = "";
                	        			String clientPrenom = "";
                	        			String group = "";
                	        			String adminNom = "";
     
     
                	        			try{
                	        				clientNom = jo.getString("customers_lastname");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				clientPrenom = jo.getString("customers_firstname");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				group = jo.getString("customers_groups");
                	        			}catch (JSONException e) {}
     
                	        			try{
                	        				adminNom = jo.getString("admin_name");
                	        			}catch (JSONException e) {}
     
     
                	        			String suggest = "2"+clientNom + " " + clientPrenom + " " + group + " " + adminNom;
                	        			suggestions.add(suggest);
                	        			listType.add(2);
     
     
                	        		}
     
     
    								else if(jo.getString("type_result").equals("prospection")){
     
    									String nom = "";
    									String prenom = "";
    									String adress = "";
     
    									try{
                	        				nom = jo.getString("nom");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				prenom = jo.getString("prenom");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				adress = jo.getString("adress");
                	        			}catch (JSONException e) {}
     
    									String suggest = "1"+nom + " " + prenom + " " + adress;
    									suggestions.add(suggest);
    									listType.add(1);
    								}
     
     
    								else if(jo.getString("type_result").equals("piges")){
    									String typeBien = "";
    									String typeTransaction = "";
    									String price = "";
    									String codePostal = "";
    									String ville = "";
    									String adminName = "";
     
    									try{
                	        				typeBien = jo.getString("type_bien");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				typeTransaction = jo.getString("TypeTransaction");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				price = jo.getString("products_price");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				codePostal = jo.getString("products_postcode");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				ville = jo.getString("products_city");
                	        			}catch (JSONException e) {}
     
    									try{
                	        				adminName = jo.getString("admin_name");
                	        			}catch (JSONException e) {}
     
     
    									String suggest = "4"+typeBien + " " + typeTransaction + "\n" + price + " " + codePostal + " " + ville + "\n" + adminName;
    									suggestions.add(suggest);
    									listType.add(4);
     
    								}
     
    								else if(jo.getString("type_result").equals("menu")){
    									String titre = "";
     
    									try{
                	        				titre = jo.getString("title");
                	        			}catch (JSONException e) {}
     
    									String suggest = "5"+titre;
    									suggestions.add(suggest);
    									listType.add(5);
     
    								}
     
     
                				}
     
                	        } catch (JSONException e) {
                				// TODO Auto-generated catch block
                				e.printStackTrace();
                			}
     
                		List<String> tri1 = new ArrayList<String>();
                		List<String> tri2 = new ArrayList<String>();
                		List<String> tri3 = new ArrayList<String>();
                		List<String> tri4 = new ArrayList<String>();
                		List<String> tri5 = new ArrayList<String>();
                		List<String> tri6 = new ArrayList<String>();
     
                		for(int i = 0; i < suggestions.size() ; i++){
                			if(listType.get(i) == 1)
                				tri1.add(suggestions.get(i));
                			if(listType.get(i) == 2)
                				tri2.add(suggestions.get(i));
                			if(listType.get(i) == 3)
                				tri3.add(suggestions.get(i));
                			if(listType.get(i) == 4)
                				tri4.add(suggestions.get(i));
                			if(listType.get(i) == 5)
                				tri5.add(suggestions.get(i));
                			if(listType.get(i) == 6)
                				tri6.add(suggestions.get(i));
                		}
     
                		listTri.clear();
     
                		listTri.addAll(tri1);
                		listTri.addAll(tri2);
                		listTri.addAll(tri3);
                		listTri.addAll(tri4);
                		listTri.addAll(tri5);
                		listTri.addAll(tri6);
     
     
     
                        // Now assign the values and count to the FilterResults
                        // object
                        filterResults.values = suggestions;
                        filterResults.count = suggestions.size();
                    }
                    return filterResults;
                }
     
    			@Override
    			protected void publishResults(CharSequence arg0, FilterResults arg1) {
    				// TODO Auto-generated method stub
     
    			}
     
            };
            return myFilter;
        }
     
    	public HashMap<Integer, String> getUrl() {
    		return url;
    	}
     
     
     
     
    }



    La methode getFilter() permet de choisir les résultats de ma suggestion.


    Comment réaliser des résultats image + text de suggestion?
    J'imagine qu'il faudra créer un nouveau fichier XML avec un TextView et une ImageView, et modifier mon adapter afin de les afficher.

    Sinon avez-vous un tuto dans le même cas que moi? J'en ai trouvé mais avec des méthodes différentes de la mienne

    Je vous remercie d'avance

  2. #2
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 61
    Par défaut
    problème réglé en adaptant la vue dans la méthode getView()!

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

Discussions similaires

  1. Ajouter image avec du texte dans JTextPane
    Par padre.cedano dans le forum Composants
    Réponses: 4
    Dernier message: 08/01/2014, 02h01
  2. Ajout d'image avec SL sharpPDF
    Par morgan2b dans le forum Silverlight
    Réponses: 0
    Dernier message: 02/04/2011, 20h53
  3. Réponses: 1
    Dernier message: 19/02/2010, 20h40
  4. [DOM] Ajouter une image avec Dom
    Par Pacen dans le forum Général JavaScript
    Réponses: 2
    Dernier message: 26/06/2008, 21h52
  5. [VBA] Ajouter image avec lien
    Par Léponge85 dans le forum Macros et VBA Excel
    Réponses: 1
    Dernier message: 24/06/2008, 15h38

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