0

I'm iterating through a cursor and populating a SparseArray with ArrayList's containing bundles of information from the cursor:

// An ArrayList to hold all of our components per section
ArrayList<ObjectKanjiLookupChar> al = new ArrayList<ObjectKanjiLookupChar>();
// We'll hold on to all of the above ArrayLists and process them at once
SparseArray<ArrayList<ObjectKanjiLookupChar>> compArray = new SparseArray<ArrayList<ObjectKanjiLookupChar>>();

do
{
    // Read values from the cursor
    int id = cursor.getInt(cursor.getColumnIndex("_id"));
    String component = cursor.getString(cursor.getColumnIndex("component"));
    int compStrokes = cursor.getInt(cursor.getColumnIndex("strokes"));

    // Create a new object for this component so we can display it in the GridView via an adapter
    ObjectKanjiLookupChar oklc = new ObjectKanjiLookupChar();
    oklc.setCharacterID(id);
    oklc.setCharacter(component);
    oklc.setStrokeCount(compStrokes);

    al.add(oklc);

    // Add headers whenever we change stroke groups
    if(compStrokes != strokesSection)
    {
        compArray.put(strokesSection, al);
        al.clear();
        strokesSection = compStrokes;
    }
}
while(cursor.moveToNext());

// Add the final group of components to the array
compArray.put(strokesSection, al);

Immediately afterwards, I iterate through the SparseArray:

for(int i = 0; i < compArray.size(); i++)
{
    Integer strokes = compArray.keyAt(i);
    ArrayList<ObjectKanjiLookupChar> alComp = compArray.get(strokes);

    // DEBUG
    Log.i("DialogKanjiLookup", "Components in Section " + strokes + ": " + alComp.size());

    ll.addView(createNewSection(String.valueOf(strokes), alComp));
}

For some unknown reason, the Log() call above reports that alComp has zero entries. I verified that ArrayList.size() was returning numbers greater than 0 when I put() them into the SparseArray, so I must be doing something incorrect when iterating through the SparseArray. What is going on?

IAmKale
  • 3,146
  • 1
  • 25
  • 46

1 Answers1

2

I suspect that the problem comes from this piece of code:

 if(compStrokes != strokesSection)
    {
        compArray.put(strokesSection, al);
        al.clear(); // Here
        strokesSection = compStrokes;
    }

You cleared the array list after you added to the SparseArray. You might think that after you have added the list to the SparseArray, SparseArray would keep a copy of the ArrayList. However, they actually share the same reference. Since you cleared the ArrayList, you cleared out the one inside SparseArray too.

The following code should fix the problem.

// We'll hold on to all of the above ArrayLists and process them at once
SparseArray<ArrayList<ObjectKanjiLookupChar>> compArray = new SparseArray<ArrayList<ObjectKanjiLookupChar>>();

do
{
    // Read values from the cursor
    int id = cursor.getInt(cursor.getColumnIndex("_id"));
    String component = cursor.getString(cursor.getColumnIndex("component"));
    int compStrokes = cursor.getInt(cursor.getColumnIndex("strokes"));

    // Create a new object for this component so we can display it in the GridView via an adapter
    ObjectKanjiLookupChar oklc = new ObjectKanjiLookupChar();
    oklc.setCharacterID(id);
    oklc.setCharacter(component);
    oklc.setStrokeCount(compStrokes);

    ArrayList<ObjectKanjiLookupChar> al = compArray.get(comStrokes);
    if(al == null) {
        al = new ArrayList<ObjectKanjiLookupChar>();
        compArray.put(comStrokes, al);
    }

    al.add(oklc);
}
while(cursor.moveToNext());
Lawrence Choy
  • 6,088
  • 1
  • 20
  • 30
  • If that's the case, then how do I preserve the `ArrayList` while still allowing for looping code? – IAmKale Sep 12 '13 at 01:51
  • that creates one ArrayList per `ObjectKanjiLookupChar`. I want the ArrayList to contain multiple `ObjectKanjiLookupChar` grouped by the value of compStroke. – IAmKale Sep 12 '13 at 02:11
  • @MasterKale ok I understood what you wanted to do :) Please see my edited code again. – Lawrence Choy Sep 12 '13 at 02:19
  • That worked great, AND it solved my problem of how to add the last group of components to the SparseArray! Thanks! – IAmKale Sep 12 '13 at 02:29