有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用Sax解析器通过按钮单击java解析数据所有按钮返回相同的数据,而不是独占数据

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

 [![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

如果您能提供任何帮助,我们将不胜感激


共 (1) 个答案

  1. # 1 楼答案

    通过在用户单击按钮执行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();
            }
        }