0

I am a beginner to android.As I want to display list view from the JSON file locally stored in that application.when i am trying this I got an error in

Error:(83, 52) error: no suitable constructor found for  ArrayAdapter(PrimaryActivity,int,ArrayList<HashMap<String,String>>)
constructor ArrayAdapter.ArrayAdapter(Context,int,int) is not applicable
(argument mismatch; ArrayList<HashMap<String,String>> cannot be converted  to int)
constructor ArrayAdapter.ArrayAdapter(Context,int,String[]) is not applicable
(argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to String[])
constructor ArrayAdapter.ArrayAdapter(Context,int,List<String>) is not applicable
(argument mismatch; ArrayList<HashMap<String,String>> cannot be converted to List<String>)

My code here

public String loadJSONFromAsset() {
    String json = null;
    try {

        InputStream is = getAssets().open("locate.json");
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");


    } catch (IOException ex) {
        ex.printStackTrace();
        return null;
    }
    return json;

}

My activity code goes here .I got the error putting the array list value into list view.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_primary);
    try {
        JSONObject obj = new JSONObject(loadJSONFromAsset());
        JSONArray jsonArray=obj.getJSONArray("Manali");
        ArrayList<HashMap<String, String>> formList= new ArrayList<HashMap<String, String>>();
        HashMap<String, String> m_li;
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jo_inside = jsonArray.getJSONObject(i);
            Log.d("Details-->", jo_inside.getString("location"));
            String location = jo_inside.getString("location");
            String url = jo_inside.getString("url");
            m_li=new HashMap<String, String>();
            m_li.put("location", location );
            m_li.put("url", url );
            formList.add(m_li);
        }

        ListView listView=(ListView)findViewById(R.id.listView1);
//got error this line:I can't understnd this line
        final ArrayAdapter<String> listAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,formList);
        listView.setAdapter(listAdapter);

    } catch (JSONException e) {
        e.printStackTrace();
    }

    Button button= (Button) findViewById(R.id.btndis);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {



        }
    });
}

My JSON data file named locate.json

{
"Manali": [
{
  "location": "Part of Manali Market",
  "url": "ps1"
},
{
  "location": "Kamaraj salai",
  "url": "ps2"
}
]
}
1

1 Answer 1

1

ArrayAdapter wants to know the type of the items inside the list, not the type of the list itself.

Replace

final ArrayAdapter<String> listAdapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,formList);

with

final ArrayAdapter<HashMap<String, String>> listAdapter = new ArrayAdapter<HashMap<String,String>>(this, android.R.layout.simple_list_item_1,formList);

EDIT: If you want to publish only the locations in the ListView, use ArrayList instead of HashMap.

        List<String> formList= new ArrayList<String>();
        try {
            JSONObject jSONObject = new JSONObject(loadJSONFromAsset());
            JSONArray jsonArray = jSONObject.getJSONArray("Manali");

            for (int i = 0; i < jsonArray.length(); i++) {
                JSONObject jo_inside = jsonArray.getJSONObject(i);
                if (jo_inside.has("location")) {
                    formList.add(jo_inside.getString("location"));
                }
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ListView listView = (ListView) findViewById(R.id.listView);
        listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, formList));
Sign up to request clarification or add additional context in comments.

1 Comment

i want to show location only my listview instead of getting stackoverflow.com/questions/28896385/…

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.