can someone help me ? I have json result like this from fileserver, and what I want to do is only taking result where "status" value is "Menunggu" to myadapter
`{"message" : "Berhasil", "result" :
[
{"unit":"Sekretariat Universitas","startdate":"25-04-2020 08.00","enddate":"25-04-2020 12.00","kegiatan":"Seminar","status":"Menunggu"},
{"unit":"Sekretariat Universitas","startdate":"23-03-2020 22:33","enddate":"23-03-2020 22:33","kegiatan":"Rapat","status":"Disetujui"},
{"unit":"Fakultas Teknik","startdate":"23-03-2020 08:00","enddate":"23-03-2020 12:00","kegiatan":"Rapat","status":"Menunggu"},
{"unit":"Fakultas Universitas","startdate":"23-03-2020 22:33","enddate":"23-03-2020 22:33","kegiatan":"Rapat","status":"Disetujui"}
]}`
And here is my adapter, I am using "equals" function on "setStatus" but it throwing error, what should I do ?
StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, ">>" + response);
try {
removeSimpleProgressDialog();
JSONObject jsonObject = new JSONObject(response);
if (jsonObject.optString("message").equals("Berhasil")) {
JSONArray dataArray = jsonObject.getJSONArray("result");
for (int i = 0; i < dataArray.length(); i++) {
JSONObject dataObj = dataArray.getJSONObject(i);
RuangModel ruangModel = new RuangModel();
ruangModel.setStatus(dataObj.optString("status").equals("Menunggu");
ruangModel.setKegiatan(dataObj.getString("kegiatan"));
ruangModel.setStartDate(dataObj.getString("startdate"));
ruangModel.setEndDate(dataObj.getString("enddate"));
ruangModel.setUnit(dataObj.getString("unit"));
ruangModelList.add(ruangModel);
}
}
adapter.notifyDataSetChanged();
} catch (JSONException e) {
e.printStackTrace();
}
}
}
And this is my RuangModel.class where I put my JSON result from fileserver
public class RuangModel {
private String id, unit, kegiatan, startDate, endDate, status;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getKegiatan() {
return kegiatan;
}
public void setKegiatan(String kegiatan) {
this.kegiatan = kegiatan;
}
public String getStartDate() {
return startDate;
}
public void setStartDate(String startDate) {
this.startDate = startDate;
}
public String getEndDate() {
return endDate;
}
public void setEndDate(String endDate) {
this.endDate = endDate;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
}
ruangModel.setStatus() will throw you an excpetion because if the status is not equal to "Menunggu" it will try to set null value in status. Instead try this approach.
for (int i = 0; i < dataArray.length(); i++) {
JSONObject dataObj = dataArray.getJSONObject(i);
RuangModel ruangModel = new RuangModel();
ruangModel.setStatus(dataObj.getString("status"));
ruangModel.setKegiatan(dataObj.getString("kegiatan"));
ruangModel.setStartDate(dataObj.getString("startdate"));
ruangModel.setEndDate(dataObj.getString("enddate"));
ruangModel.setUnit(dataObj.getString("unit"));
//Here you check the status and if its equal to "Menunggu" you add it to the list. Otherwise it will not be added to the list.
if(ruangModel.getStatus().equals("Menunggu")){
ruangModelList.add(ruangModel);
}
}