0

I have this method that adds elements to a GetterSetter class and the add them to an arrayList with is added to a listView. The thing is in the listView it only shows the last items from the array multiple times.
I don't understand why? Any solution?

Here is my code:

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();
}

 }

Here is my catLog:

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.....]

Here is Adapter Code:

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

1 Answer 1

3

addValues = new GetterSetter(); you are using one object, and adding one same object mulitiple times.

so you should create GetterSetter object in your for loop

Because you have to crete same amount of object, as they are in your parsed object/array

Sign up to request clarification or add additional context in comments.

2 Comments

Yesss! Thank you! It was in the for loop this morning though, i think i moved it out with the arrayList out of the loop :D. I didn't notice!
Yes i will but i have to wait 3 minutes :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.