How to create Cursor from JSONArray?

android, android-cursor, android-listview, arrays

Solution

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

You can convert that `JSONArray` to a `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*/});
}

Problem

I have an adapter class which is extending `GroupingCursorAdapter` and constructor of type `Adapter_Contacts(Context context, Cursor cursor, AsyncContactImageLoader asyncContactImageLoader)`. I want to use this same class for populating my `ListView`. I am getting data from one web service which is `JSON`. So my question is that, how can I convert a `JSONArray` to `Cursor` to use same adapter class?

Original source