android应用程序中的getter和setter方法出错

时间:2013-07-14 19:31:55

标签: android eclipse xml-parsing getter-setter

我正在使用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” 并显示修复“创建字段,创建常量”。

0 个答案:

没有答案