javaandroidhashmapandroid-arrayadaptersimpleadapter

Populate Data to Listview but Arrayadapter not working


I am trying to populate the data from my arrayList to listview. But for some reason I get the error error: no suitable constructor found for ArrayAdapter(activity2.SendPostRequest,int,int,ArrayList<HashMap<String,String>>) constructor ArrayAdapter.ArrayAdapter(Context,int,int,HashMap<String,String>[]) is not applicable

                JSONArray json = new JSONArray(data);
                ArrayList<HashMap<String, String>>  arrayList = new ArrayList<HashMap<String, String>>();

                HashMap<String, String> map = new HashMap<String, String>();

                try {
                for(int i=0;i<json.length();i++){
                    JSONObject e =json.getJSONObject(i);

                    map.put("id", String.valueOf(i));
                    map.put("Name", "Vorname: " + e.getString("meta_value"));
                    map.put("orderid", "id: " + e.getString("post_id"));
                    arrayList.add(map);
                    return map.toString();
                }
                    ArrayAdapter<HashMap<String, String>> adapter = new ArrayAdapter<HashMap<String, String>>(this,android.R.layout.simple_list_item_1, android.R.id.text1, arrayList);

                list.setAdapter(adapter);
                }catch (JSONException e){
                    Log.e(this.getClass().getName(), "Some JSON error occurred" + e.getMessage());

                }

Cant find anywhere the solution


Solution

  • ArrayAdapter(activity2.SendPostRequest,int,int,ArrayList>)

    Problem is you pass this as the first parameter for giving context to the adapter.

    constructor ArrayAdapter.ArrayAdapter(Context,int,int,HashMap<String,String>[])

    Solution

    Instead of this pass this

    getContext() or getActivity()

    because this may point to different contexts depends on were you using.

    Refer to this link to get insights about thiskeyword