1

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:

enter image description here

2
  • can u state the exact error that you get !! Commented May 5, 2016 at 13:18
  • Thanks for the reply! I just posted a screenshot of the error. Commented May 5, 2016 at 13:21

1 Answer 1

1

Try getActivity() in place of this

SimpleAdapter simpleAdapter = new SimpleAdapter(getActivity(), employeeList,
                android.R.layout.simple_list_item_1,
                new String[] { "orders" }, new int[] { android.R.id.text1 });
        listView.setAdapter(simpleAdapter);

It is also important to understand why this won't wok. The first parameter needs to be context. If this was an activity this might have worked because this refers to the parent object and in an activity it will give you context. But inside fragment you cannot use this to get the context. You can get context inside fragment by calling getACtivity(). In different situations you will have to make sure that you are provding the right context !!

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.