Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9092731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:54:32+00:00 2026-06-16T22:54:32+00:00

i am adding images and data obtained from webservice(json response) in listview. Some times

  • 0

i am adding images and data obtained from webservice(json response) in listview. Some times images are present in array and some times no images, if images are present in array then i am getting correct output both image and data will display in listview but if no images in array then even though data is present but it will not display anything in listview. struggling here with if statement following is my code Any answers will be appreciated can any one help?

    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);
                // to remove all <P> </p> and <br /> and replace with ""
                 content = content.replace("<br />", "");
                 content = content.replace("<p>", "");
                 content = content.replace("</p>", "");

                //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(android.R.id.list);
    list.setOnScrollListener(this);
    // Getting adapter by passing json data ArrayList
    adapter=new LazyAdapter(this, songsList);        
    list.setAdapter(adapter);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:54:33+00:00Added an answer on June 16, 2026 at 10:54 pm

    It is because you are doing everything inside one single try-catch block. When there has no image it will throw an error, jumping out of your loop. You should add a try-catch block alone for reading the images:

        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);
                    // to remove all <P> </p> and <br /> and replace with ""
                     content = content.replace("<br />", "");
                     content = content.replace("<p>", "");
                     content = content.replace("</p>", "");
    
                    //authornumber is agin  JSON Object
                    JSONObject author = c.getJSONObject(KEY_AUTHOR);
                    String name = author.getString(KEY_NAME);
    
                    String url = null;
                    String slug = null;
                    try {
                    JSONArray atta = c.getJSONArray("attachments");
                    for(int j = 0; j < atta.length(); j++){
                        JSONObject d = atta.getJSONObject(j);
    
                        slug = d.getString(KEY_SLUG);
    
                        JSONObject images = d.getJSONObject(KEY_IMAGES);
    
                        JSONObject thumbnail = images.getJSONObject(KEY_THUMB_URL);
                        url = thumbnail.getString(KEY_URL);
                    } catch (Exception e) {}
    
    
    
    
            // 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(android.R.id.list);
        list.setOnScrollListener(this);
        // Getting adapter by passing json data ArrayList
        adapter=new LazyAdapter(this, songsList);        
        list.setAdapter(adapter);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After doing a lot of research posting this question. Adding Images from json data
I am getting data from JSON Array and I can show the text in
I am adding images to a JPanel but the images are getting cut off.
I am trying to upload some images and adding them div container. My question
I am confused How to use looping for Json response Array in another Array.
In my application I download some xml data file from some information center, add
i have a problem while incrementing a variable and adding new data to array
I have 2 ways of adding images to a table and, which is most
In my app I am dynamically adding images to my view at runtime. I
I've found this question on StackOverflow Adding div below images in colorbox But it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.