Search code examples
javaandroidxmlandroid-studiosaxparser

Parsing data with Sax Parser via button click all buttons return the same data instead of the exclusive data


Hi everyone this question is similar to a question I answered a few days ago for another user found here. I am displaying the parsed data in a list view the first button I click will successfully display its information however all buttons after that will simply display the same data the first button returned in that question i solved this by setting result to equal nothing in the onclick method but that does not appear to be solving it here. I have provided code and an image of the application below.

 [![public void onClick(View aview) {
        if (aview == incidentButton) {
            startIncidentProgress();
        }
        if (aview == roadButton) {
            startRoadworksProgress();
        }
        if (aview == plannedButton) {
            startPlannedProgress();
        }
    }

    public void startIncidentProgress() {
        // Run network access on a separate thread;
        new Thread(new Task(url1)).start();
    }

    public void startRoadworksProgress() {
        // Run network access on a separate thread;
        new Thread(new Task(url2)).start();
    }

    public void startPlannedProgress() {
        // Run network access on a separate thread;
        new Thread(new Task(url3)).start();
    } //


    class Task implements Runnable {
        private String url;


        public Task(String aurl) {
            url = aurl;
        }

        @Override
        public void run() {

            URL aurl;
            URLConnection yc;
            BufferedReader in = null;
            String inputLine = "";


            Log.e("MyTag", "in run");

            try {
                Log.e("MyTag", "in try");
                aurl = new URL(url);
                yc = aurl.openConnection();
                in = new BufferedReader(new InputStreamReader(yc.getInputStream()));
                //
                // Throw away the first 2 header lines before parsing
                //
                //
                //
                while ((inputLine = in.readLine()) != null) {
                    result = result + inputLine;
                    Log.e("MyTag", inputLine);
                }
                in.close();
            } catch (IOException ae) {
                Log.e("MyTag", "ioexception");
            }

            MainActivity.this.runOnUiThread(new Runnable() {
                public void run() {
                    Log.d("UI thread", "I am the UI thread");
                    try{
                        ListView lv = (ListView) findViewById(R.id.listView1);
                        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
                        SAXParser parser = parserFactory.newSAXParser();
                        DefaultHandler handler = new DefaultHandler(){
                            String currentValue = "";
                            boolean currentElement = false;
                            public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException {
                                currentElement = true;
                                currentValue = "";
                                if(localName.equals("item")){
                                    rss = new HashMap<>();
                                }
                            }
                            public void endElement(String uri, String localName, String qName) throws SAXException {
                                currentElement = false;
                                if (localName.equalsIgnoreCase("title"))
                                    rss.put("title", currentValue);
                                else if (localName.equalsIgnoreCase("description"))
                                    rss.put("description", currentValue);
                                else if (localName.equalsIgnoreCase("link"))
                                    rss.put("link", currentValue);
                                else if (localName.equalsIgnoreCase("georss:point"))
                                    rss.put("georss:point", currentValue);
                                else if (localName.equalsIgnoreCase("author"))
                                    rss.put("author", currentValue);
                                else if (localName.equalsIgnoreCase("comments"))
                                    rss.put("comments", currentValue);
                                else if (localName.equalsIgnoreCase("pubDate"))
                                    rss.put("pubDate", currentValue);
                                else if (localName.equalsIgnoreCase("item"))
                                    RSSList.add(rss);
                            }
                            @Override
                            public void characters(char\[\] ch, int start, int length) throws SAXException {
                                if (currentElement) {
                                    currentValue = currentValue +  new String(ch, start, length);
                                }
                            }
                        };
                        parser.parse(new InputSource(new StringReader(result)), handler);
                        ListAdapter adapter = new SimpleAdapter(MainActivity.this, RSSList, R.layout.list_row,new String\[\]{"title","description","link","georss:point","author","comments","pubDate"},
                                new int\[\]{R.id.title, R.id.description, R.id.link, R.id.georss, R.id.author, R.id.comments, R.id.pubdate});
                        lv.setAdapter(adapter);
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    } catch (ParserConfigurationException e) {
                        e.printStackTrace();
                    } catch (SAXException e) {
                        e.printStackTrace();
                    }
                }
            });
        }
    }
}

image of application

any help you could offer would be appreciated.


Solution

  • Fixed this by clearing the contents of the list when the user clicks the buttons to execute the XML calls. In doing so the list view repopulates with the correct information instead of the same information. Code below.

    public void onClick(View aview) {
            if (aview == incidentButton) {
                result = "";
                RSSList.clear();
                startIncidentProgress();
            }
            if (aview == roadButton) {
                result = "";
                RSSList.clear();
                startRoadworksProgress();
            }
            if (aview == plannedButton) {
                result = "";
                RSSList.clear();
                startPlannedProgress();
            }
        }