2017-08-11 2 views
0

Je souhaite afficher les paires clé/valeur d'une ArrayMap avec un ArrayAdapter dans un GridView avec deux colonnes.ArrayMap avec ArrayAdapter dans Gridview

ArrayMap<String, String> testmap = new ArrayMap<String, String>(); 
    testmap.put("one", "eins"); 
    testmap.put("two", "drei"); 
    testmap.put("three", "vier"); 
    testmap.put("four", "fünf"); 
    testmap.put("five", "sechs"); 

    ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, testmap); 
    GridView gridView = (GridView) findViewById(R.id.list); 
    gridView.setAdapter(itemsAdapter); 

Ceci est le message d'erreur:

Error:(49, 45) error: no suitable constructor found for 
ArrayAdapter(NumbersActivity,int,ArrayMap<String,String>) 
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not   applicable 
(argument mismatch; ArrayMap<String,String> cannot be converted to int) 
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable 
(argument mismatch; ArrayMap<String,String> cannot be converted to String[]) 
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable 
(argument mismatch; ArrayMap<String,String> cannot be converted to List<String>) 

Merci pour l'aide!

+0

Avez-vous lu votre message d'erreur? – Surreal

+0

Oui, je l'ai fait et je ne suis pas assez compétent pour y arriver. C'est pourquoi je demande. J'ai besoin d'un autre constructeur ... Mais lequel? –

Répondre

0

Vous devez créer une liste de carte pour utiliser ArrayAdapter

ArrayList<ArrayMap<String, String>> testmap = new ArrayList<>(); 

    ArrayMap<String, String> map = new ArrayMap<>(); 
    map.put("one", "eins"); 
    map.put("two", "drei"); 
    map.put("three", "vier"); 
    map.put("four", "fünf"); 
    map.put("five", "sechs"); 

    testmap.add(map); 

    ArrayAdapter<ArrayMap<String, String>> itemsAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, testmap); 
    gridView.setAdapter(itemsAdapter); 

Et afin de préserver l'insertion des valeurs dans votre carte, utilisez LinkedHashMap

EDIT

Pour afficher valeurs dans une cellule différente, créez une nouvelle carte chaque fois que vous y mettez des valeurs

ArrayMap<String, String> map = new ArrayMap<>(); 
    map.put("one", "eins"); 
    testmap.add(map); 
    map = new ArrayMap<>(); 
    map.put("two", "drei"); 
    testmap.add(map); 
    map = new ArrayMap<>(); 
    map.put("three", "vier"); 
    testmap.add(map); 
    map = new ArrayMap<>(); 
    map.put("four", "fünf"); 
    testmap.add(map); 
    map = new ArrayMap<>(); 
    map.put("five", "sechs"); 
    testmap.add(map); 
+0

Merci pour votre réponse! ArrayAdapter ne prend qu'un seul argument. –

+0

Veuillez vérifier l'édition –

+0

Vous avez résolu le problème lié au message d'erreur. Maintenant, les cinq paires clé/valeur sont affichées dans une cellule du GridView: {one = eins, two = drei, ...} –