I have a JSON string that looks like this:
{"orders":[{"orderid":"561","order_date":"2016-05-04 09:23:18"},{"orderid":"560","order_date":"2016-05-04 08:56:33"}],"success":1}
And I want to display it inside a ListView in a Fragment Activity. Here's the code to this fragment:
public class PastOrders extends Fragment {
private String jsonResult;
private String url = "https://www.example.com/orders.php";
private ListView listView;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.past_orders, container, false);
listView = (ListView) v.findViewById(R.id.listView1);
accessWebService();
return v;
}
// Async Task to access the web
private class JsonReadTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HashMap<String,String> data = new HashMap<>();
data.put("user_id",params[0]);
data.put("status",params[1]);
RegisterUserClass ruc = new RegisterUserClass();
String result = ruc.sendPostRequest(url,data);
jsonResult = result;
return null;
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
// e.printStackTrace();
Toast.makeText(getActivity().getApplicationContext(),
"Error..." + e.toString(), Toast.LENGTH_LONG).show();
}
return answer;
}
@Override
protected void onPostExecute(String result) {
ListDrwaer();
}
}// end async task
public void accessWebService() {
JsonReadTask task = new JsonReadTask();
// passes values for the urls string array
task.execute(new String[] { url });
}
// build hash set for list view
public void ListDrwaer() {
List<Map<String, String>> employeeList = new ArrayList<Map<String, String>>();
try {
JSONObject jsonResponse = new JSONObject(jsonResult);
JSONArray jsonMainNode = jsonResponse.optJSONArray("orders");
for (int i = 0; i < jsonMainNode.length(); i++) {
JSONObject jsonChildNode = jsonMainNode.getJSONObject(i);
String name = jsonChildNode.optString("orderid");
String number = jsonChildNode.optString("order_date");
String outPut = name + "-" + number;
employeeList.add(createEmployee("orders", outPut));
}
} catch (JSONException e) {
Toast.makeText(getActivity().getApplicationContext(), "Error" + e.toString(),
Toast.LENGTH_SHORT).show();
}
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "orders" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
}
private HashMap<String, String> createEmployee(String name, String number) {
HashMap<String, String> employeeNameNo = new HashMap<String, String>();
employeeNameNo.put(name, number);
return employeeNameNo;
}
}
I'm having errors in this following lines:
SimpleAdapter simpleAdapter = new SimpleAdapter(this, employeeList,
android.R.layout.simple_list_item_1,
new String[] { "orders" }, new int[] { android.R.id.text1 });
listView.setAdapter(simpleAdapter);
It say that SimpleAdapter cannot be applied to this class that I'm using.
This is the error that I am getting:
