I have a table from which i have to get rows which have KEY_DATE = dat.
I am then saving these rows as objects and returning a list of those objects.
The problem is that if I have 4 entries in my table, then instead of getting all 4 rows, I’m getting the last row 4 times.
Here is my code…
while(!l.isEmpty())
{
hd = l.get(0);
l.remove(0);
map = new HashMap<String, String>();
Log.w("myapp",hd._name+hd._price+hd._qty);
map.put("item", hd._name);
map.put("price", Double.toString(hd._price));
map.put("qty", Integer.toString(hd._qty));
map.put("total", Double.toString(hd._price*hd._qty));
mylist.add(n++,map);
}
SimpleAdapter mSchedule = new SimpleAdapter(getActivity(), mylist, R.layout.details_item_list, new String[] {"item", "price", "qty" , "total"}, new int[] {R.id.col1, R.id.col2, R.id.col3, R.id.col4});
list.setAdapter(mSchedule);
The items are being retrived , but my listview is showing the last item only mutliplied by the number of items in my sql table . Am i using the map.put() and add() function in loop correctly .
That’s because you only have one instance of
HistoryDetails, which you add 4 times inside your List, modifying it on each iteration. You need to puthd = new HistoryDetails();inside your loop, like