2016-03-30 48 views
-1

所以我有這樣的代碼:不將項目添加到ArrayAdapter

suggestionsAdapter.clear(); 
       locations.clear(); 
       List<Predictions> predictions = gson.fromJson(jsonObject.getJSONArray("predictions").toString(), new TypeToken<List<Predictions>>(){}.getType()); 
       for(Predictions prediction : predictions){ 
        String title = prediction.terms.get(0).value; 
        locations.add(title); 
        suggestionsAdapter.add(title); 
       } 
       Log.i("","loc are:" + locations); 
       Log.i("","locations are:" + locations.size()); 
       suggestionsAdapter.notifyDataSetChanged(); 
       Log.i("","suggestion adapter size:" + suggestionsAdapter.getCount()); 

而且我回來的:

03-30 10:55:56.474: I/(3185): loc are:[Albert Cuypstraat, Albert Cuyp Market, Albania, Alblasserdam, Albert Cuypstraat] 
03-30 10:55:56.474: I/(3185): locations are:5 
03-30 10:55:56.474: I/(3185): suggestion adapter size:0 

這究竟是爲什麼? PS:這發生在一個偵聽器內部,該偵聽器從Google地方查詢中獲取預測結果。 更改爲:

suggestionsAdapter.addAll(locations); 
         suggestionsAdapter.notifyDataSetChanged(); 

還是同樣的結果,有時我得到:

03-30 11:03:25.876: I/(10409): loc are:[Albert Cuypstraat, Albert Cuyp Market, Albert Cuypstraat, AH Haarlemmerdijk, Albert Heijn] 
03-30 11:03:25.876: I/(10409): locations are:5 
03-30 11:03:25.876: I/(10409): suggestion adapter size:3 
+0

爲什麼你在suggestionsAdapter.add(...)中添加一個標題? –

+0

原因我對suggestionsAdapter.addAll(locations)獲得了相同的結果;我說我會嘗試單獨添加它們 –

+0

只是調用'notifyDataSetChanged();''在SuggestedAdapter.add(...)內' –

回答