如何从JSONArray创建Cursor?

ast*_*ter 9 arrays android cursor android-listview

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

我想用同一个类来填充我的ListView.我从一个Web服务获取数据JSON.

所以我的问题是,我怎么能转换JSONArrayCursor使用相同的适配器类?

Luk*_*rog 8

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

您可以将其转换JSONArrayMatrixCursor:

// 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*/});
}
Run Code Online (Sandbox Code Playgroud)