Android:Sax解析为列表视图
问题描述:
首先,我是Android和Java的新手。我一直在试图添加一个“简单”Rss阅读器到我的应用程序。我希望它做的是解析来自特定站点的RSS提要,并获取标题和发布日期,并将这些项目放入列表视图中,当单击列表项时,将打开浏览器以显示包含该文章的网页。够简单吗?我很伤心地说,现在我已经陷入了几天的困境。Android:Sax解析为列表视图
我读过一些教程/例子,我试图修改,以适应我的目的看起来像代码:
主要活动
public class News extends Activity {
private final String MY_DEBUG_TAG = "Some NEWS";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a new TextView to display the parsingresult later. */
TextView tv = new TextView(this);
try {
/* Create a URL we want to load some xml-data from. */
URL url = new URL("http://www.some.com/feed/");
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
ParsedExampleDataSet parsedExampleDataSet =
myExampleHandler.getParsedData();
/* Set the result to be displayed in our GUI. */
tv.setText(parsedExampleDataSet.toTitle());
} catch (Exception e) {
/* Display any Error to the GUI. */
tv.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "NewsQueryError", e);
}
/* Display the TextView. */
this.setContentView(tv);
}
}
ExampleHandler
public class ExampleHandler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private boolean in_entry = false;
private boolean in_id = false;
private boolean in_title = false;
private boolean in_updated = false;
private boolean in_summary = false;
private boolean in_link = false;
private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("entry")) {
this.in_entry = true;
}else if (localName.equals("id")) {
this.in_id = true;
}else if (localName.equals("title")) {
this.in_title = true;
}else if (localName.equals("updated")){
this.in_updated = true;
}else if (localName.equals("summary")) {
this.in_summary = true;
}else if (localName.equals("link")) {
this.in_link = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("entry")) {
this.in_entry = false;
}else if (localName.equals("id")) {
this.in_id = false;
}else if (localName.equals("title")) {
this.in_title = false;
}else if (localName.equals("updated")) {
this.in_updated = false;
}else if (localName.equals("summary")) {
this.in_summary = false;
}else if (localName.equals("link")) {
this.in_link = false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
public void characters(char ch[], int start, int length) {
if(this.in_title){
myParsedExampleDataSet.setextractedTitle(new String(ch, start, length));
}
}
}
ParsedExampleDataSet
public class ParsedExampleDataSet {
private String extractedId = null;
private String extractedTitle = null;
private String extractedUpdated = null;
private String extractedSummary = null;
private String extractedImage = null;
private int extractedInt = 0;
public String getextractedId() {
return extractedId;
}
public void setextractedId(String extractedId) {
this.extractedId = extractedId;
}
public String getextractedTitle() {
return extractedTitle;
}
public void setextractedTitle(String extractedTitle) {
this.extractedTitle = extractedTitle;
}
public String getextractedUpdated() {
return extractedUpdated;
}
public void setextractedUpdated(String extractedUpdated) {
this.extractedUpdated = extractedUpdated;
}
public String getextractedSummary() {
return extractedSummary;
}
public void setextractedSummary(String extractedSummary) {
this.extractedSummary = extractedSummary;
}
public String toId(){
return this.extractedId;
}
public String toTitle(){
return this.extractedTitle;
}
public String toUpdated(){
return this.extractedUpdated;
}
public String toSummary(){
return this.extractedSummary;
}
}
现在当然这个w生病只是返回Feed中的最后一个条目。它正在解析正确,因为我可以获得我想单独显示的每个元素。对于如何实施清单,我无能为力。
在此先感谢
答
让我们一步一步来,希望我们可以达到你想要做什么。我已经包含了您需要了解的API文档的相关链接。
- 对于ListView来说,DOM可能是一个更简单的选择,因为ListView在列表上运行,可以很好地处理结构化列表。对于刚接触Android和Java的人来说,学习也更容易。请参阅IBM tutorial for XML关于“基于DOM的feed解析器的实现”部分。下面是你应该关心代码的例子
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.getInputStream());
Element root = dom.getDocumentElement();
- 部分在这一点上,你有你的ListView数据。 ListView通过使用适配器来工作。适配器是一个负责 a。设置初始数据和b。显示每行应该如何。因此,这个类对于使ListView工作很重要
- 现在,您将创建自己的扩展名为BaseAdapter,它将采用之前获得的根元素。我在这里提供了一个框架类:
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class RSSAdapter extends BaseAdapter {
Element rootElement;
public RSSAdapter(Element rootElement) {
this.rootElement = rootElement;
}
public int getCount() {
// get the count of children of the root element
return 0;
}
public Object getItem(int position) {
// get the child identified by the position,
// this is the important part where you
// want to get the right child representing the RSS data that you want to get
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// implement your view here....
return null;
}
}
- 如果你不熟悉的元素,我建议你看看下面的Element API Documentation。您需要正确识别上面的getItem()函数中的孩子
- 现在您已准备好实施getView()方法。在这里,您将基本上完成下面的操作
- 可以访问您要使用getItem()时,你已经实现
- 创建每一行的XML布局,在这里夸大他们的子节点。如果你不熟悉Android设备上查看,看看View API Documentation,因为这是要知道
- 从子节点提取数据,并将它设置为您相应的视图元素
- 下一个重要的事情,你需要设置在您的ListView上添加OnItemClickListener。在监听器中,您希望使用getItem()再次获取XML(此方法对您至关重要)并获取链接。
- 最后,在的onClick()的实施,要使用WebView并加载URL
这应该足以让你实现你想要的。如果步骤不明确,我会进一步澄清。希望这可以帮助。
+0
只是访问,发现这个意想不到的伟大答案:) –
你是否发布了所有这些代码以询问如何实现ListView? –
是的,我想我看到很多人被告知他们没有提供足够的代码。对不起,如果在这种情况下会更少。 –