I am trying to retrieve all the database and create a and individual JSON for each row eg:
{ "id":"1", "name":"John"}
{ "id":"2", "name":"Mick"}
{ "id":"3", "name":"Tom"}
This is my select code in my DBQuery.java file to fetch the users:
public ArrayList<HashMap<String, String>> getUser(){
ArrayList<HashMap<String, String>>userArrayList = new ArrayList<HashMap<String, String>>();
String selectQuery = "SELECT * FROM user ORDER BY name";
SQLiteDatabase database = this.getWritableDatabase();
Cursor cursor = database.rawQuery(selectQuery, null);
if(cursor.moveToFirst()){
do{
HashMap<String, String> contactMap = new HashMap<String, String>();
contactMap.put("d", cursor.getString(0));
contactMap.put("name", cursor.getString(1));
userArrayList.add(contactMap);
} while(cursor.moveToNext());
}
return userArrayList;
}
And this is my code so far for the JSON object:
public class user {
private String id;
private String name;
public user (String id, String name) {
this.name = id;
this.gender = name;
}
public String toJson() {
Gson gson = new Gson();
String json = gson.toJson(this);
return json;
}
public static user constructFromJson(String json) {
Gson gson = new Gson();
return gson.fromJson(json, user.class);
}
}
I am now completely lost and don't know how to finish it.