2017-08-11 6 views
0

2つの列を持つGridViewにArrayAdapterを持つArrayMapのキーと値のペアを表示したいとします。ArrayAdapter with 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); 

これはエラーメッセージです:助けを

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>) 

ありがとう!あなたは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); 

して表示するLinkedHashMap

EDIT

を使用し、マップ内の値の挿入を維持するためにを使用するマップのリストを作成する必要が

+0

エラーメッセージをお読みになりましたか? – Surreal

+0

はい、私はそれを解決するのに十分な熟練者ではありませんでした。それが私が尋ねる理由です。私は別のコンストラクタが必要です...しかし、どちらか? –

答えて

0

値を入力するたびに新しいマップを作成する

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

ありがとうございました! ArrayAdapterは引数を1つだけとります。 –

+0

編集内容を確認してください –

+0

エラーメッセージに関する問題を解決しました。 GridViewの1つのセルに5つのキーと値のペアが表示されます。{1 = eins、two = drei、...} –