2

有时 json 数组与图像 url 一起出现,有时它是空的。我正在使用以下代码,其中带有图像 url 的数组在列表视图中显示数据和图像,但是当数组为空时,数据也不会显示。我该如何解决这个问题?任何答案将不胜感激

    ArrayList<HashMap<String, String>> songsList = new ArrayList<HashMap<String, String>>();


    // Creating JSON Parser instance
            JSONParser jParser = new JSONParser();

            // getting JSON string from URL
            JSONObject json = jParser.getJSONFromUrl(URL);
            try {
                JSONArray posts = json.getJSONArray(KEY_POSTS);

    // looping through all song nodes <song>

            for(int i = 0; i < posts.length(); i++){
                JSONObject c = posts.getJSONObject(i);
                // Storing each json item in variable
                String id = c.getString(KEY_ID);
                String title = c.getString(KEY_TITLE);
                String date = c.getString(KEY_DATE);
                String content = c.getString(KEY_CONTENT);

                //authornumber is agin  JSON Object
                JSONObject author = c.getJSONObject(KEY_AUTHOR);
                String name = author.getString(KEY_NAME);


                JSONArray atta = c.getJSONArray("attachments");
                for(int j = 0; j < atta.length(); j++){
                    JSONObject d = atta.getJSONObject(j);

                    String slug = d.getString(KEY_SLUG);

                    JSONObject images = d.getJSONObject(KEY_IMAGES);

                    JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                    String url = thumbnail.getString(KEY_URL);



        // creating new HashMap
        HashMap<String, String> map = new HashMap<String, String>();

        // adding each child node to HashMap key => value
        map.put(KEY_ID, id);
        map.put(KEY_TITLE, title);
        map.put(KEY_DATE, date);
        map.put(KEY_NAME, name);
        map.put(KEY_CONTENT, content);
        map.put(KEY_SLUG, slug);
        map.put(KEY_URL, url);


        // adding HashList to ArrayList
        songsList.add(map);
            }   
            } catch (JSONException e) {
                e.printStackTrace();

                }


    list=(ListView)findViewById(R.id.list);

    // Getting adapter by passing json data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);
4

0 回答 0