I want to populate a listview from my json which looks like this:
{ library: { books: [ { name: "name", autor: "autor",year: "year"}, { name: "name", autor: "autor", year: "year"}], library: { name: "name"}}}
In main activity i have this method
public void books() {
request = new JsonRequestClass(getContext(), JsonMethodClass.GET, null);
try {
String response = request.execute("http://booksjson").get();
ResponseListModel responseListModel = new ResponseListModel(response);
if (responseListModel.isSuccess() == true) {
} else {
responseListModel.setSuccess(false);
responseListModel.setMessage("Error");
//Toast.makeText(getContext(), responseListModel.getMessage(), Toast.LENGTH_LONG).show();
}
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
}
this method help me to read the json but how do i populate the listview if i have a void method and in my adapter class is waiting for an arraylist from model class?
public BookAdapter(Context context, List<Book> list) {
this.context = context;
this.list = list;
}
I-m adding my ResponseListModel model class
public class ResponseListModel {
private boolean success;
private String message;
private JSONArray list;
private JSONObject object;
public ResponseListModel(String pObjetc) {
try {
JSONObject object = new JSONObject(pObjetc);
this.message = object.getString("message");
this.success = object.getBoolean("success");
//response.setMessage(object.optString("message"));
String strModel = object.getString("strModel");
String type = object.optString("type");
if (type.equals("array")) {
this.list = object.getJSONArray("model");// new JSONArray(strModel);
} else
object = object.getJSONObject("model");
} catch (JSONException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
public boolean isSuccess() {
return success;
}
public void setSuccess(boolean success) {
this.success = success;
}
public String getMessage() {
return message;
}
public JSONArray getList() {
return list;
}
public JSONObject getObject() {
return object;
}
public void setMessage(String message) {
this.message = message;
}
public void setList(JSONArray list) {
this.list = list;
}
public void setObject(JSONObject object) {
this.object = object;
}
}
And to be more specific i need help on this line on my main
adapter = new BookAdapter(this, books);