我正在使用一个 android 应用程序,并在 getter 和 setter 方法中遇到错误:
SitesList 类型中的方法 setItem(ArrayList) 不适用于参数 (String)
它在我的MyXmlHandler文件中显示了上述错误:siteList.setItem(currentValue)
这是我的MyXmlHandler文件:
package com.example.svnitupdates;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class MyXmlHandler extends DefaultHandler {
Boolean currentElement = false;
String currentValue = null;
public static SitesList siteList = null;
public static SitesList getSitesList() {
return siteList;
}
public static void setSitesList(SitesList sitesList) {
MyXmlHandler.siteList = sitesList;
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
currentElement = true;
if (localName.equals("maintag"))
{
/** Start */
siteList = new SitesList();
}
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
currentElement = false;
/** set value */
if (localName.equalsIgnoreCase("item"))
siteList.setItem(currentValue);
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
if (currentElement) {
currentValue = new String(ch, start, length);
currentElement = false;
}
}
}
这是我的SiteList.java
package com.example.svnitupdates;
import java.util.ArrayList;
import com.example.svnitupdates.R.string;
public class SitesList {
private ArrayList<String> item = new ArrayList<String>();
private ArrayList<String> chapter = new ArrayList<String>();
public ArrayList<String> getItem() {
return item;
}
public void setItem(ArrayList<String> item) {
this.item = item;
}
public ArrayList<String> getChapter() {
return chapter;
}
public void setChapter(ArrayList<String> chapter) {
this.chapter = chapter;
}
}
这是我的UpdatesActivity.java
package com.example.svnitupdates;
import java.net.URL;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
public class UpdatesActivity extends Activity
{
private ListView listview;
SitesList siteList = null;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.updates);
listview = (ListView) findViewById(R.id.updatesListView);
final ArrayList<String> list = new ArrayList<String>();
try {
/** Handling XML */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader();
/** Send URL to parse XML Tags */
URL sourceUrl = new URL(
"http://localhost/svnit%20updates/updates.xml");
MyXmlHandler myXMLHandler = new MyXmlHandler();
xr.setContentHandler(myXMLHandler);
xr.parse(new InputSource(sourceUrl.openStream()));
}catch (Exception e)
{
System.out.println("XML Parsing Excpetion = " + e);
}
siteList = MyXmlHandler.siteList;
for (int i = 0; i < siteList.getItem().size(); ++i)
{
list.add(siteList.getItem.get(i));
}
final UpdatesAdapter adapter = new UpdatesAdapter(UpdatesActivity.this,
android.R.layout.simple_list_item_1, list);
listview.setAdapter(adapter);
listview.setItemsCanFocus(false);
listview.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Intent i = new Intent(UpdatesActivity.this, ArticleActivity.class);
i.putExtra("s", position);
startActivity(i);
}
});
}
}
在上面的代码中,它在行中显示错误:list.add(siteList.getItem.get(i))
在“ .getItem
”处并显示修复“创建字段,创建常量”。