Reputation: 5854
I'm facing some known solvable problem but struck out here. In my code, the values are getting stored in ArrayList on the first for loop execution. However, on the 2nd loop and further, the values are overwritten by the final values in the ArrayList. Finally, the lastly entered values are alone getting stored with the number of times of size of the list.
lstall = (ListView)findViewById(R.id.lvall);
sampleArrayList = new ArrayList<HashMap<String, String>>();
ListAdapter sampleListAdapter;
dh = new DBHelper(this);
HashMap<String, String> sampleObjectMap;
List<String> lvall = dh.selectAll();
sampleObjectMap= new HashMap<String, String>();
for(int i=0;i<lvall.size();i++)
{
for (@SuppressWarnings("unused") String sampleObj : lvall)
{
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
}
sampleArrayList.add(sampleObjectMap);
}
I need to store all the values in a arraylist and display within a list view. Any help is highly appreciated and thanks in advance.
Upvotes: 0
Views: 404
Reputation: 1500535
You're creating a single HashMap<String, String>
and repeatedly overwriting the entries within it.
In fact, you're doing that twice as you have a nested for loop for no obvious reason. I believe you want:
for (int i = 0; i < lvall.size(); i++)
{
HashMap<String, String> sampleObjectMap = new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
It also seems odd to use a list's size but not actually use the values within the list... you might want to consider restructuring your code...
Upvotes: 3
Reputation: 63698
All the elements in the list have the reference of the same Map
instance and you are overriding the same Map
entries for each iteration.
for(int i=0;i<lvall.size();i++) {
// move it inside the loop
sampleObjectMap= new HashMap<String, String>();
But i don't think that you need a Map
here. Just create a class with all the required fields as member variables.
Upvotes: 1
Reputation: 128428
Try it out:
for(int i=0;i<lvall.size();i++)
{
sampleObjectMap= new HashMap<String, String>();
sampleObjectMap.put("title", dh.val1(i));
sampleObjectMap.put("person", dh.pers(i));
sampleObjectMap.put("priorty", setpriority(String.valueOf(dh.prioirty(i))));
sampleObjectMap.put("dat", getDate(Long.valueOf(dh.time(i)),"dd/MM/yyyy"));
sampleArrayList.add(sampleObjectMap);
}
Upvotes: 1
Reputation: 31453
I have not tested it, but try to create new Map
sampleObjectMap = new HashMap<String, String>();
in your inner loop, before you do put. Test it. Later try to use 1 map object and calling clear() after the map is recorded in the sampleArrayList.
Upvotes: 0