通过按钮使用 Sax Parser 解析数据单击所有按钮 return 相同的数据而不是独占数据

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

大家好,这个问题类似于我几天前为另一位用户here 回答的问题。我在列表视图中显示解析的数据我点击的第一个按钮将成功显示其信息但是之后的所有按钮将只显示相同的数据在该问题中返回的第一个按钮我通过在 onclick 中将结果设置为不等于任何内容来解决这个问题方法,但这似乎并没有在这里解决它。我在下面提供了应用程序的代码和图片。

 [![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();
                    }
                }
            });
        }
    }
}

如有任何帮助,我们将不胜感激。

通过在用户单击按钮执行 XML 调用时清除列表内容来解决此问题。这样做时,列表视图会重新填充正确的信息而不是相同的信息。下面的代码。

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