Android MergeCursor打破版本> 3.X?

时间:2012-01-04 18:45:49

标签: android cursor android-cursoradapter

有没有人使用过这个类,看看它是否在Android 3.x及以上版本中被破坏了?在以前的版本中,它对我来说很好。这是一个例子:

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MatrixCursor cursorOne = new MatrixCursor(new String[]{ "_id", "name", "description" }); 

cursorOne.addRow(new Object[]{ 5, "Object 2", "Description 2" }); 
cursorOne.addRow(new Object[]{ 5, "Object 3", "Description 3" }); 
cursorOne.addRow(new Object[]{ 5, "Object 4", "Description 4" }); 
cursorOne.addRow(new Object[]{ 5, "Object 5", "Description 5" }); 

CursorAdapter cursorAdapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_2, cursorOne, new String[] { "name", "description" }, new int []{ android.R.id.text1, android.R.id.text2 }); 

MatrixCursor cursorTwo = new MatrixCursor(new String[]{ "_id", "name", "description" }); 

cursorTwo.addRow(new Object[]{ 5, "Object 2", "Description 2" }); 
cursorTwo.addRow(new Object[]{ 5, "Object 3", "Description 3" }); 
cursorTwo.addRow(new Object[]{ 5, "Object 4", "Description 4" }); 
cursorTwo.addRow(new Object[]{ 5, "Object 5", "Description 5" }); 

MergeCursor mergeCursor = new MergeCursor(new Cursor[] { cursorOne, cursorTwo }); 

cursorAdapter.changeCursor(mergeCursor);

setListAdapter(cursorAdapter); 
}

它没有显示任何内容,似乎它正在清理游标。我错过了SDK>的内容吗? 3.X?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案:

我替换了兼容性包中的CursorAdapterSimpleCursorAdapter导入,并使用了CursorAdapter.swapCursor而不是CursorAdapter.changeCursor

似乎是swapCursor没有关闭上一个光标的相关内容。