如何从数组列表中的android显示列表视图数组、视图、列表、列表中

2023-09-04 03:03:48 作者:奔跑吧暖男

我是一个初学者android.As我想显示从本地存储在application.when我想这个我在

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

我的code此处

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;

}

我的活动code到这里。我得到了错误把数组列表值到列表视图。

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



        }
    });
}

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

My JSON data file named locate.json

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

推荐答案

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

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

替换

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

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