JSON String is incorrectly mapped to textviews

前端 未结 1 876
旧时难觅i
旧时难觅i 2021-01-29 09:54

I have 3 textviews which I\'m attempting to populate with data from a JSON Http response however they are currently being populated with the incorrect data from the response.

1条回答
  •  不知归路
    2021-01-29 10:30

    You have

    CommentsLibrary lib = new CommentsLibrary(jsonString, jsonString, jsonString);
    

    All params passed to the constructor of CommentsLibrary is jsonString. All the three have the same jsonstring.

    public CommentsLibrary(String name, String content, String published) {
            this.name = name;
            this.content = content;
            this.published = published;
        }
    

    So its the same content that is displayed when you get method for the corresponding fields

    Edit:

    Declare list as a instance variable

     ArrayList list = new ArrayList();
    

    Then

         for (int i = 0; i < jsonArray.length(); i++) {
         JSONObject jsonObject = jsonArray.getJSONObject(i);
         String name = jsonObject.optString("name","defaultValue");
         String content = jsonObject.optString("content","defaultValue");
         String published = jsonObject.optString("published","defaultValue");
         list.add(new CommentsLibrary(name, content, published));
         }
    

    Then in onPostExecute

    Use a ListView witha custom adapter and populate the listview with the ArrayList list.

    With text view you only see 1 set of values.

    0 讨论(0)
提交回复
热议问题