如何在 android 中显示来自 arraylist 的列表视图

How to display listview from arraylist in android

我是 android.As 的初学者 我想显示本地存储在 JSON 文件中的列表视图 application.when 我正在尝试这个 我在 [=15] 中遇到错误=]

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>)

我的代码在这里

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;

}

我的 activity 代码在这里。我在将数组列表值放入列表视图时出错。

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) {



        }
    });
}

我的 JSON 数据文件名为 locate.json

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

ArrayAdapter 想知道列表中项目的类型,而不是列表本身的类型。

替换

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

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

编辑: 如果您只想发布 ListView 中的位置,请使用 ArrayList 而不是 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));