1

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

2 Answers 2

1

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);
3
  • @ArunA So the list view is still empty? Jan 9, 2013 at 8:40
  • If attachments array is empty then listview will be empty, if no images then atleast i need to display its data
    – Tycoon
    Jan 9, 2013 at 9:33
  • 1
    @ArunA I have modified the answer, can you try to use the full code above and tell us what is the result? Jan 9, 2013 at 9:55
1

In your listAdapter's getView() method check for url value. if its empty or null, then display some stub image( some image like No image available-> keep it in res).

4
  • let me know if this doesn't help or you are having some other problem.
    – SKK
    Jan 9, 2013 at 6:52
  • if its resulting in an application crash, then paste the logcat.
    – SKK
    Jan 9, 2013 at 8:33
  • No crash but if the attachments arry is empty then its data will not display in listview
    – Tycoon
    Jan 9, 2013 at 9:32
  • 1
    okay. in the adapter class use try/catch to check for url and setting the imageview. do this at the end after assigning values for everything else.
    – SKK
    Jan 9, 2013 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.