的Android SAX解析器处理程序
问题描述:
我使用SAX解析器来开发一个XML分析程序。的Android SAX解析器处理程序
我必须遵循以下XML提要:
<root>
<Categories>
<Category name="Photos">
<article articleid="4537" title="Sonam-Steals-the-Mai-Show"/>
<article articleid="4560" title="Big-B-Croons-For-World-Peace"/>
<article articleid="4585" title="Yami-Paints-The-Town-Red"/>
</Category>
<Category name="Style">
<article articleid="266" title="Dita wows us in a sari"/>
<article articleid="268" title="Frieda is a natural"/>
<article articleid="269" title="Demi finds love in Jodhpur"/>
</Category>
</Categories>
</root>
在这里,我已经创建getter和setter一个类:
public class Laptop {
private String brand;
private List<String> model;
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public List<String> getModel() {
return model;
}
public void setModel(List<String> string) {
this.model = string;
}
我的XML处理程序看起来像下面的代码:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
current = true;
if (localName.equals("Category")) {
laptop = new Laptop();
laptop.setBrand(attributes.getValue("name"));
}
else if (localName.equals("article")) {
laptop.setModel(attributes.getValue("title"));
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if(current)
{
currentValue = new String(ch, start, length);
current=false;
}
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
current = false;
if (localName.equals("Category")) {
// add it to the list
laptops.add(laptop);
}
if (localName.equals("article")) {
laptop.getModel().add(currentValue);
}
这里我得到下面的错误对这些行:
laptop.setModel(attributes.getValue("title"));
在笔记本型的方法则setModel(名单)不适用于
我怎样才能解决error.please的参数(字符串)给我这些解决方案...
答
错误说冯不能assing一个String
实例的List
实例。这是相当合理的。由于
public void setModel(List<String> string) {
this.model = string;
}
以List为参数。
加以修正,你可以尝试以这种方式,如果你想保持String
的List
一(条),每笔记本实例
public void setModel(String string) {
if (this.model == null)
this.model = new List<String>();
this.model.add(string);
}
答
至于@blackbelt试图解释,你只能分配值这被一种方法所接受。 TextView.setText(CharSequence)
接受CharSequence
所以你应该提供一个字符串,而getModel()返回字符串List<String>
答
我不是激励你的代码像这样的名单,但你可避免模型类和投内部处理程序数据类型类似如下:
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
db = new DatabaseHelper(thecontext);
if (qName.equals("Asa.Amms.Data.Entity.User")) {
int length = attributes.getLength();
for (int i = 0; i < length; i++) {
String name = attributes.getQName(i);
if (name.equals("Id")) {
id = Integer.parseInt(attributes.getValue(i));
}
if (name.equals("Login")) {
LoginID = attributes.getValue(i).toString();
}
if (name.equals("Name")) {
Name = attributes.getValue(i).toString();
}
if (name.equals("Password")) {
Password = attributes.getValue(i).toString();
}
if (name.equals("ProgramOfficerId")) {
user_ProgramOfficerId = Integer.parseInt(attributes.getValue(i).toString());
}
}
db.insertUser(id, LoginID, Name, Password, user_ProgramOfficerId);
}
}
更改私人表 model;以私人字符串模型; –
2013-02-27 09:24:34
@blackbelt解决上面error.but获取下面的错误TextText类型中的方法setText(CharSequence)不适用于这些行上的参数 \t(列表)holder.txtModel.setText(laptop.getModel()); –
user2098063
2013-02-27 09:32:23
是相同的错误。如果一个方法需要一个字符串作为参数,你不能将它传递给一个字符串列表。选择需要显示的字符串并从列表中获取。你应该真的阅读文档,但首先你应该清楚自己想要达到什么以及如何实现。这些只是建议 – Blackbelt 2013-02-27 09:37:56