android - 如何从 JSONArray 创建 Cursor?

标签 android arrays android-listview android-cursor

我有一个适配器类,它扩展了 GroupingCursorAdapter 和类型的构造函数 Adapter_Contacts(Context context, Cursor cursor, AsyncContactImageLoader asyncContactImageLoader)

我想使用同一个类来填充我的 ListView。我从一个网络服务获取数据,它是 JSON

所以我的问题是,如何将 JSONArray 转换为 Cursor 以使用相同的适配器类?

最佳答案

So my question is that, how can I convert a JSONArray to Cursor to use same adapter class?

您可以将 JSONArray 转换为 MatrixCursor :

// I'm assuming that the JSONArray will contain only JSONObjects with the same propertties
MatrixCursor mc = new MatrixCursor(new String[] {"columnName1", "columnName2", /* etc*/}); // properties from the JSONObjects
for (int i = 0; i < jsonArray.length(); i++) {
      JSONObject jo = jsonArray.getJSONObject(i);
      // extract the properties from the JSONObject and use it with the addRow() method below 
      mc.addRow(new Object[] {property1, property2, /* etc*/});
}

关于android - 如何从 JSONArray 创建 Cursor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20797323/

相关文章:

java - Android如何搜索对GitHub的依赖关系?

javascript - React Native 文本输入掩码

c# - C# 中的快速字节数组屏蔽

javascript - 是否有与 Java 的 deepToString() 等效的 JavaScript?

java - 如何执行调用 ListView 项目上的特定号码

android - 由于 Lollipop 中的 'void dalvik.system.CloseGuard.close()' 导致的 NullPointerException

android - 如何使除了我在 RecyclerView 中单击的项目之外的所有项目都不可见?

javascript - 列表 Javascript 输出不正确

java - ListView 正在检查我的 RadioButtons 而没有被触及

java - 尝试使用按钮传递项目和子项目