Search code examples
androidlistadapterarrays

How to set raw array in list using base adapter in android?


I am developing an android application where I have an raw Jsonarray like -

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

I have to use this Jsonarray in list Adapter which have an Image and an text.

Help me I am new to android.


Solution

  • First thing Change your JSON to

    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
        }
    ]
    

    then store JSON TO VARIABLE as String and parse it like below.

    SOLUTION :

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