我有一个适配器类,它扩展了 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/