【问题标题】:Create a cursor from hardcoded array instead of DB从硬编码数组而不是 DB 创建游标
【发布时间】:2013-08-19 21:30:14
【问题描述】:

我正在尝试为我正在编写的一个小游戏应用程序制作一个拖放列表。

列表中有 6 个条目。但是,我添加的库需要一个与数据库对话的 Cursor 对象。这对我的情况来说太过分了。

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

谢谢

【问题讨论】:

    标签: android android-cursor


    【解决方案1】:

    查看MatrixCursor documentation。检查例如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);
    

    【讨论】:

    • ,@FaddishWorm 感谢您的帖子。你能告诉我为什么我得到 startManagingCursor(matrixCursor) 的“方法的返回类型丢失”错误。什么解决方案来解决这个问题。我正在使用 API 19,最低 API 支持为 8。
    • @FaddishWorm:我解决了这个问题。这是一个愚蠢的错误。我在错误的地方打电话,即。不在类的任何方法中。我设法将它作为 getActivity().startManagingCursor(matrixCursor) 工作,因为该类是一个片段。
    • 如果有完整的代码就好了。 IE 那个布局是你做的???我只是在这里测试库,我需要一个带字符串的光标
    【解决方案2】:

    也许你可以查看MatrixCursor 类,你可以调用addRow((Iterable<?> columnValues)addRow(Object[] columnValues) 希望会有所帮助

    【讨论】:

      【解决方案3】:

      使用MatrixCursor,而不是不太方便的addRow(),使用builder方法newRow()

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2016-12-06
        • 2021-07-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多