0

I have a custom listView with 200 values coming from Cloud DB. All the values are received properly and added in ArrayList(hotelList). When I am passing to the ArrayList(hotelList) to Custom Adapter class, showing all the values as last element. Many stackOverflow questions found here related to this I tried them too. None worked for me. How should I fix this? The code is,

@Override
 protected void onPostExecute(String result) {
 try {
 Log.d("Inside Post", "message");
 root = new JSONObject(result);
 validation = root.getInt("response");
 message = root.getString("message");
 JSONArray data=root.getJSONArray("data");
 data.length();
 if(validation==1) {
 hgs=new HotelGetSetter();//Getting and setting class
 for(int i=0;i<data.length();i++){
 hgs.setName(data.getJSONObject(i).getString("hotel_name"));
 hgs.setPlace(data.getJSONObject(i).getString("city"));
 hotelList.add(hgs);//While Debugging, all the 200 values from db received and added in the ArrayList(hotelList) values also there correctly
 }
 //But after the loop all the values here changed to last element value(debugged).
 mainMethod();
 }
 }
 catch (JSONException e) {
 e.printStackTrace();
 }
 }
 public void mainMethod(){
 HotelCustomAdapter hca=new HotelCustomAdapter(HotelMaster.this,hotelList);
 list_hotel.setAdapter(hca);
 //list_hotel.setAdapter(new HotelCustomAdapter(this, hotel_id1,hotel_name1,hotel_place1));
}
asked Jan 23, 2016 at 12:19
3
  • I'm honestly not understanding your question, but looking at your code it looks strange to me that you are building your adapter in post execute. You should build outside and in post execute just invoke adapter.notifyDataSetChanged for having new items on bottom of the list. Commented Jan 23, 2016 at 12:26
  • @thetonrifles, Please check the question once. I added comments in the code. After the for loop the (ArrayList<HotelGetSetter> hotelList) has the last value for data.length() times. Commented Jan 23, 2016 at 12:37
  • you initialize variable hgs=new HotelGetSetter(); out of for loop and then you are always editing the same instance. Commented Jan 23, 2016 at 12:42

2 Answers 2

2

you initialize variable hgs=new HotelGetSetter(); out of for loop and then you are always editing the same instance

answered Jan 23, 2016 at 12:43
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you thetonrifles, hypd09
1

You are changing the values for the same object hsg through the loop instead of creating different objects for each set of values. Therefore the 200 same objects.

Just move the hgs=new HotelGetSetter(); into the loop and it should work.

answered Jan 23, 2016 at 12:42

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.