gpt4 book ai didi

Android:Sax 解析为 ListView

转载 作者:行者123 更新时间:2023-11-29 22:21:13 25 4
gpt4 key购买 nike

首先,我是 Android 和 Java 的新手。我一直在尝试将“简单”的 Rss 阅读器添加到我的应用程序中。我想要它做的就是解析来自特定站点的 RSS 提要并获取标题和发布日期并将这些项目放入 ListView 中,当单击列表项目时它将打开浏览器以显示包含该文章的网页。够简单吧?很遗憾地说,这让我困惑了几天。

我已经阅读了一些教程/示例,我正在尝试修改以适合我的目的的代码如下所示:
主要 Activity

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

示例处理器

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

解析示例数据集

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

当然,这只会返回提要中的最后一个条目。它正在正确解析,因为我可以获得要单独显示的每个元素。我对如何实现 listveiw 一无所知。

提前致谢

最佳答案

让我们一步一步来,希望我们能实现您想要做的事情。我已经包含了您需要了解的 API 文档的相关链接。

  • 对于 ListView,DOM 可能是一个更简单的选择,因为 ListView 在一个列表上运行,该列表可以很好地与结构化列表一起工作。对于刚接触 Android 和 Java 的人来说,它也更容易学习。看这个IBM tutorial for XML在“提要解析器的基于 DOM 的实现”部分。以下是示例中您应该注意的代码部分


DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.getInputStream());
Element root = dom.getDocumentElement();

  • 此时您拥有了 ListView 的数据. ListView 通过使用 Adapter 来工作。适配器负责A。设置初始数据和 b。显示每一行的外观。因此,此类对于使 ListView 正常工作很重要
  • 现在您要创建自己的 BaseAdapter 扩展这将采用您之前获得的根元素。我在这里提供了一个骨架类:

<p></p>

<pre><code>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;
}
}
</code></pre>

<p></p>

  • 如果你对Element不熟悉,建议你看看下面的Element API Documentation .您需要在上面的 getItem() 函数中正确识别 child
  • 现在您已准备好实现 getView() 方法。在这里你基本上会做以下步骤
    • 使用您已实现的 getItem() 访问您想要的子节点
    • 为每一行创建一个 XML 布局并在此处扩充它们。如果您不熟悉 Android View,请查看 View API Documentation因为这是一件重要的事情要知道
    • 从子节点中提取数据并将其设置到您对应的View元素
  • 接下来,您需要设置 OnItemClickListener在你的 ListView 上。在监听器中,您可能希望使用 getItem()(此方法对您至关重要)再次获取 XML 并获取链接。
  • 最后,在onClick() 的实现中,您想使用WebView。并加载 URL

这应该足以让您实现您想要的。如果步骤不清楚,我会进一步澄清。希望这会有所帮助。

关于Android:Sax 解析为 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com