2015-11-21 108 views
0

我有這種方法將元素添加到GetterSetter類中,並將它們添加到listList中並添加到listView中。事情是在listView中,它只是多次顯示數組中的最後一項。
我不明白爲什麼?任何解決方案只有最後一個元素在listView中多次顯示

這裏是我的代碼:

protected void onPostExecute(String str) { 

    myArrayList = new ArrayList<GetterSetter>(); 
    addValues = new GetterSetter(); 
    String rating="-NA-"; 
    try { 

     JSONObject root = new JSONObject(str); 
     JSONArray results = root.getJSONArray("results"); 
     for (int i = 0; i < results.length(); i++) { 
      JSONObject arrayItems = results.getJSONObject(i); 
      JSONObject geometry = arrayItems.getJSONObject("geometry"); 
      JSONObject location = geometry.getJSONObject("location"); 

      if(!arrayItems.isNull("rating")){ 
       rating = arrayItems.getString("rating"); 
      } 

      addValues.setLat(location.getString("lat")); 
      addValues.setLon(location.getString("lng")); 
      addValues.setName(arrayItems.getString("name").toString()); 
      addValues.setRating(rating); 
      addValues.setVicinity(arrayItems.getString("vicinity").toString()); 

      Log.d("Before", myArrayList.toString()); 

      myArrayList.add(addValues); 

     } 
    } catch (Exception e) { 

    } 
    System.out 
      .println("############################################################################"); 
    Log.d("After:", myArrayList.toString()); 
    nodata = (TextView) findViewById(R.id.nodata); 
    nodata.setVisibility(View.GONE); 
    adapter = new CustomAdapter(ListActivity.this, R.layout.list_row, myArrayList); 
    myList.setAdapter(adapter); 
    //adapter.notifyDataSetChanged(); 
    dialog.dismiss(); 
} 

} 

這裏是我的目錄下載:

11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕ [] 
11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before 
    [Stefano's4.3El-Gaish Road] 
11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕ 
    [McDonald's4.3Smouha, Victor Emanouil Al Thaleth, Qism Sidi Gabir, 
    McDonald's4.3Smouha, Victor Emanouil Al Thaleth, Qism Sidi Gabir] 
11-21 21:13:03.845 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕ 
    [Sudoku Cafe سودوكو كافيه4Alexandria, 49 Mostafa Kamel street 49 شارع 
    مصطفي كامل, Smouha, Sudoku Cafe سودوكو كافيه4Alexandria, 49 Mostafa Kamel 
    street 49 شارع مصطفي كامل, Smouha, Sudoku Cafe سودوكو كافيه4Alexandria, 
    49 Mostafa Kamel street 49 شارع مصطفي كامل, Smouha] 
11-21 21:13:03.848 32431-32431/pfe.com.neighborhoodserviceslbs D/Before﹕ 
    [Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, 
    Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 
    21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 
    Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe 
    & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe & 
    Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber] 
11-21 21:13:03.850 32431-32431/pfe.com.neighborhoodserviceslbs I/System.out﹕ 
    ############################################################################ 
11-21 21:13:03.850 32431-32431/pfe.com.neighborhoodserviceslbs D/After:﹕ 
    [Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, 
    Club 21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 
    21 Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 
    Cafe & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Cafe 
    & Restaurant4.3Mustafa Kamel WA Bolkli, Qesm Sidi Gaber, Club 21 Ca.....] 

這裏是適配器代碼:

public class CustomAdapter extends ArrayAdapter<GetterSetter> { 

Context context; 
ArrayList<GetterSetter> placesArray; 
int textViewResourceId; 

public CustomAdapter(Context context, int textViewResourceId, List<GetterSetter> objects) { 
    super(context, textViewResourceId, objects); 
    this.placesArray = (ArrayList<GetterSetter>) objects; 
    this.context = context; 
    this.textViewResourceId = textViewResourceId; 

} 

@Override public View getView(int position, View convertView, ViewGroup parent) { 
    View row = convertView; 
    TextViewHolder holder = null; 

    if (row == null) { 
     LayoutInflater inflater = ((Activity) context).getLayoutInflater(); 
     row = inflater.inflate(textViewResourceId, parent, false); 

     holder = new TextViewHolder(); 
     holder.name = (TextView) row.findViewById(R.id.name); 
     holder.rating = (TextView) row.findViewById(R.id.rating); 
     holder.ratingText = (TextView) row.findViewById(R.id.ratingRext); 
     row.setTag(holder); 
    } else { 
     holder = (TextViewHolder) row.getTag(); 
    } 

    holder.name.setText(placesArray.get(position).name); 
    holder.rating.setText(placesArray.get(position).rating); 

    return row; 
} 

static class TextViewHolder { 
    TextView name; 
    TextView rating; 
    TextView ratingText; 
} 
    } 
+0

顯示適配器代碼 –

+0

適配器代碼添加 – Lotus91

回答

2

addValues = new GetterSetter();你使用一個對象,並添加一個相同的對象多次。

所以你應該在你的for loop

創建GetterSetter對象,因爲你必須克里特島相同數量的對象,因爲他們是你的解析對象/數組中

+0

YESSS!謝謝!今天早上它在for循環中,我想我把它移出循環外的arrayList:D。我沒有注意到! – Lotus91

+0

是的,但我必須等3分鐘:) – Lotus91

相關問題