2011-06-02 117 views

回答

4

這裏試試這個

在你想要的ListView控件是在活動:

public class listView extends ListActivity { 
    ArrayList<String> items = new ArrayList<String>(); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listViewLayout); 
     try { 
      /** Handling XML */ 
      SAXParserFactory spf = SAXParserFactory.newInstance(); 
      SAXParser sp = spf.newSAXParser(); 
      XMLReader xr = sp.getXMLReader(); 

      /** If using remote server for xml file */ 
      URL sourceUrl = new URL("http://www.server.com/fileToParse.xml"); 

      /** Create handler to handle XML Tags (extends DefaultHandler) */ 
      ExampleHandler myXMLHandler = new ExampleHandler(); 
      xr.setContentHandler(myXMLHandler); 
      /** if using remote file */ 
      xr.parse(new InputSource(sourceUrl.openStream())); 
      /** if local file */ 
      xr.parse(new InputSource(getContext().getResources().getXml(R.fileToParse.MyXml)); 
     }catch(Exception e) { 
      Log.e("XML Error "+e.toString()); 
     } 

     /** Get result from MyXMLHandler SitlesList Object */ 
     sitesList = ExampleHandler.sitesList; 
     for(int i = 0; i < sitesList.getName().size(); i++) { 
      items.add(sitesList.getName().get(i)+" "+sitesList.getLastName().get(i)); 
     } 

     /** Set the layout view to display */ 
     setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); 
    } 

    public void onListItemClick(ListView parent, View v, int position, long id) { 
     /** puExtra method for if you want to pass variable to next activity */ 
     Intent i = new Intent(this, listViewSub.class); 
     i.putExtra("name", sitesList.getName()); 
     startActivity(i); 
    } 
} 

listViewLayout.xml: Can't get layout file to show so click here

ExampleHandler.java:

public class ExampleHandler extends DefaultHandler{ 

    Boolean currentElement = false; 
    String currentValue = null; 
    public static SitesList sitesList = null; 

    public static SitesList getSitesList() { 
    return sitesList; 
    } 

    public static void setSitesList(SitesList sitesList) { 
    ExampleHandler.sitesList = sitesList; 
    } 

    /** Called when tag starts (ex:- <name>AndroidPeople</name> 
    * -- <name>)*/ 
    @Override 
    public void startElement(String uri, String localName, String qName, 
    Attributes attributes) throws SAXException { 

    currentElement = true; 

    if (localName.equals("Records")) 
    { 
    /** Start */ 
    sitesList = new SitesList(); 
    } 

    } 

    /** Called when tag closing (ex:- <name>AndroidPeople</name> 
    * -- </name>)*/ 
    @Override 
    public void endElement(String uri, String localName, String qName) 
    throws SAXException { 

    currentElement = false; 

    /** set value */ 
    if (localName.equalsIgnoreCase("FirstName")) 
    sitesList.setName(currentValue); 
    else if(localName.equalsIgnoreCase("LastName")) 
    sitesList.setLastName(currentValue); 
    } 

    /** Called to get tag characters (ex:- <name>AndroidPeople</name> 
    * -- to get AndroidPeople Character) */ 
    @Override 
    public void characters(char[] ch, int start, int length) 
    throws SAXException { 

    if (currentElement) { 
    currentValue = new String(ch, start, length); 
    currentElement = false; 
    } 

    } 
} 

SitesList.java:

public class SitesList { 

/** Variables */ 
private ArrayList<String> name = new ArrayList<String>(); 
private ArrayList<String> lastName = new ArrayList<String>(); 

/** In Setter method default it will return arraylist 
* change that to add */ 

public ArrayList<String> getName() { 
return name; 
} 

public void setName(String name) { 
this.name.add(name); 
} 

public void setLastName(String lastName) { 
    this.lastName.add(lastName); 
} 

public ArrayList<String> getLastName() { 
    return lastName; 
} 

} 

fileToParse.xml:Click here for xml file

+0

如果使用遠程文件解析不要忘記以下權限添加到您的清單文件「android.permission.INTERNET對」 – camelCaseD 2011-07-06 05:44:25

0

聲明xml中的滾動視圖& relativelayout,因爲它是孩子。現在你可以添加它們來創建你想要顯示的表格行數(動態地)添加數據到它&添加這些表格行到relativealayout.So會得到listview的感覺。我用這種方式&工作出色。

相關問題