Domanda

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?

È stato utile?

Soluzione

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());
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top