为什么我的SparseArray返回一个零元素的ArrayList?

时间:2013-09-12 01:34:10

标签: android arraylist sparse-array

我正在遍历游标并使用SparseArray填充ArrayList,其中包含来自游标的信息包:

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

之后,我立即遍历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));
}

由于某些未知原因,上面的Log()来电报告alComp条目。我确认ArrayList.size()在我put()进入SparseArray时返回的数字大于0,所以在迭代SparseArray时我必须做错误的事情。发生了什么事?

1 个答案:

答案 0 :(得分:2)

我怀疑问题来自这段代码:

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

添加到SparseArray后清除了数组列表。您可能认为在将列表添加到SparseArray之后,SparseArray将保留ArrayList的副本。但是,它们实际上共享相同的参考。由于您清除了ArrayList,因此您也清除了SparseArray中的那个。

以下代码应解决问题。

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