2013-08-17 43 views
36

我正在尝试为我正在编写的一款小游戏应用做一个拖放列表。从硬编码数组而不是数据库创建游标

列表中有6个条目。但是我添加的库需要一个与数据库交谈的Cursor对象。这对我的情况是过分的。

有没有一种方法可以创建基于基于内存的数据结构(如数组)的Cursor对象?有没有一种方法可以将硬编码数组用作我的光标?

感谢

回答

46

退房的MatrixCursordocumentation。检查例如this example

String[] columns = new String[] { "_id", "item", "description" }; 

MatrixCursor matrixCursor= new MatrixCursor(columns); 
startManagingCursor(matrixCursor); 

matrixCursor.addRow(new Object[] { 1, "Item A", "...." }); 

SimpleCursorAdapter adapter = 
     new SimpleCursorAdapter(this, R.layout.layout_row, matrixCursor, ...); 

setListAdapter(adapter); 
+0

,@ FaddishWorm谢谢你的职位。你会好心告诉我,为什么我得到“返回类型的方法是失踪”错误的startManagingCursor(matrixCursor)。什么是解决这个问题的解决方案。我使用API​​ 19,最低api支持8。 – Dexter

+0

@FaddishWorm:我解决了这个问题。这是一个愚蠢的错误。我在错误的地方打了电话。不在班级的任何方法内。我设法将它作为getActivity()。startManagingCursor(matrixCursor)作为类是Fragment。 – Dexter

+0

这将是很好的完整的代码。 IE的布局是你做的东西?我只是在这里测试库,我需要一个带有字符串的游标 – StarWind0

2

也许你可以检查MatrixCursor类,您可以拨打addRow((Iterable<?> columnValues)addRow(Object[] columnValues) 希望这将有助于

1

使用MatrixCursor,而不是addRow(),这是不是很方便,使用构建器方法NEWROW()

相关问题