2013-10-02 20 views
-1

我正在開發一個android應用程序,它從Rss Feeds獲取數據並將其顯示在ListView中。 現在我設法成功解析了標題並將其顯示在ListView中。問題是,當我點擊ListView項目(即冠軍),我需要加載的描述,但我不知道如何分析它,請幫助我..如何解析Android中的Rss Feed的描述

這是我的RSSItem頁:

public class RssItem { 
// item title 
    private String title; 
    // item link 
    private String link; 
    private String description; 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public String getLink() { 
     return link; 
    } 

    public void setLink(String link) { 
     this.link = link; 
    } 

    @Override 
    public String toString() { 
     return title; 
    } 

    public String getDescription() { 
     return description; 
    } 

    public void setDescription(String description) { 
     this.description = description; 
    } 

}

這是我的Rssparse處理器頁

public class RssParseHandler extends DefaultHandler { 

private List<RssItem> rssItems; 

// Used to reference item while parsing 
private RssItem currentItem; 

// Parsing title indicator 
private boolean parsingTitle; 
// Parsing link indicator 
private boolean parsingLink; 

public RssParseHandler() { 
    rssItems = new ArrayList<RssItem>(); 
} 

public List<RssItem> getItems() { 
    return rssItems; 
} 

@Override 
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { 
    if ("item".equals(qName)) { 
     currentItem = new RssItem(); 
    } else if ("title".equals(qName)) { 
     parsingTitle = true; 
    } else if ("link".equals(qName)) { 
     parsingLink = true; 
    } 

} 

@Override 
public void endElement(String uri, String localName, String qName) throws SAXException { 

    if ("item".equals(qName)) { 
     rssItems.add(currentItem); 
     currentItem = null; 
    } else if ("title".equals(qName)) { 
     parsingTitle = false; 
    } else if ("link".equals(qName)) { 
     parsingLink = false; 
    } else if ("description".equals(qName)) { 

    } 
} 

@Override 
public void characters(char[] ch, int start, int length) throws SAXException { 
    if (parsingTitle) { 
     if (currentItem != null) 
      currentItem.setTitle(new String(ch, start, length)); 
    } else if (parsingLink) { 
     if (currentItem != null) { 
      currentItem.setLink(new String(ch, start, length)); 
      parsingLink = false; 
     } 
    } 
} 

}

這個頁面顯示的項目被點擊時的描述

public class Openpage extends Activity { 
WebView wb=null; 
String s1=null; 

@SuppressLint({ "SetJavaScriptEnabled", "HandlerLeak" }) 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.newsfeed); 
     Intent i=getIntent(); 
     s1=i.getStringExtra("link"); 

     wb=(WebView)findViewById(R.id.webView1); 
     wb.loadUrl(s1); 

wb.setWebChromeClient(new WebChromeClient() { 
     public void onProgressChanged(WebView view, int progress) { 
     if(progress == 100){ 
     // Page has fully loaded. Cancel the progress dialog here 
     } 
    } 
    }); 
} 
class CustomWebViewClient extends WebViewClient { 
     public boolean shouldOverrideUrlLoading(WebView view, String url) 
     { 
      //do whatever you want with the url that is clicked inside the webview. 
      //for example tell the webview to load that url. 
      view.loadUrl(s1); 
      //return true if this method handled the link event 
      //or false otherwise 
      return true; 
     } 
    } 

}

+0

與解析標題或鏈接有什麼不同? – njzk2

回答

0

,應該是很簡單的,但你可以發佈什麼XML \ JSON您使用的是解析器?

本質上,您應該使用xml \ json解析器從feed中獲取所有信息,feed本身可能是一個項目列表,因此使用解析器您將選擇API來獲取每個項目數據 - 實際上它沒有不同獲取Feed的標題和名稱只是一個簡單的純文本。然後將每個列表視圖項目實例化爲一個提要類,並在單擊該類時獲取信息。

因爲我已經這樣做了,所以我可以在今天晚些時候發佈我的github配置文件的完整工作代碼。