0

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.

4 Answers 4

3

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

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

3 Comments

@PareshMayani: I've also moved the declaration of the variable into the loop for the sake of limiting scope. I think it was adding the actual explanation that helped though.
@Downvoter: Care to comment? What do you object to about this answer?
Thanks a lot... This was the main wrong i was doing... perfectly worked
1

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

}

Comments

1

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.

Comments

0

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.

Comments

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.