如何在android中使用基础适配器在列表中设置原始数组?

时间:2014-12-30 05:37:33

标签: android listadapter arrays

我正在开发一个Android应用程序,我有一个原始的Jsonarray,如 -

[{"profilePic":"url","firstName":"Hitesh","lastName":"Matnani","status":0};   
{"profilePic":"url2","firstName":"Daljeet","lastName":"Singh","status":1}]

我必须在列表适配器中使用此Jsonarray,它具有图像和文本。

帮助我,我是android的新手。

1 个答案:

答案 0 :(得分:1)

首先将您的JSON更改为

JSON:

[
    {
        "profilePic": "url",
        "firstName": "Hitesh",
        "lastName": "Matnani",
        "status": 0
    }, <-- remove semicolon(;) from here.. there must be comma (,)
    {
        "profilePic": "url2",
        "firstName": "Daljeet",
        "lastName": "Singh",
        "status": 1
    }
]

然后将JSON TO VARIABLE存储为String并解析它,如下所示。

解决方案:

try {
    String response = "YOUR JSON STRING";
    JSONArray result = new JSONArray (response);

        for (int i = 0; i < result .length(); i++) {
        JSONObject obj = result.getJSONObject(i);
            String profilePic= ""+obj.getString("profilePic");
            String firstName= ""+obj.getString("firstName");
            String lastName= ""+obj.getString("lastName");
            String status= ""+obj.getInt("status");

            // do code for adding these values to adapter.
        }
    }

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