实际上,我尝试使用 SAX 解析器解析包含一些细节的简单 XML,并在 Spinner 中显示结果。但我无法确定我的问题中的问题是什么,我的意思是可能是语法错误或我的程序不正确。谁能帮我解决这个问题。
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="178dp" />
<Button
android:id="@+id/button1"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/spinner1"
android:layout_alignParentTop="true"
android:layout_alignRight="@+id/spinner1"
android:layout_marginTop="17dp"
android:text="Parse XML using SAX" />
</RelativeLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/title"
style="@style/spinner" />
样式.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="@android:style/android:Theme" />
<style name="spinner">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:textColor">#0000CD</item>
<item name="android:text">15dp</item>
<item name="android:typeface">monospace</item>
<item name="android:maxHeight">35dp</item>
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">8dp</item>
</style>
</resources>
Mainactivity.class
public abstract class MainActivity extends Activity implements OnClickListener,OnItemSelectedListener {
Button button;
Spinner spinner;
List<Item> item = null;
static final String URL = "http://www.androidituts.com/source/tutorials.xml";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
button.setOnClickListener(this);
}
private void findViewById() {
// TODO Auto-generated method stub
button = (Button) findViewById(R.id.button1);
spinner = (Spinner) findViewById(R.id.spinner1);
}
public void onClick(View v) {
item = SAXXMLParser.parse(URL);
ArrayAdapter<Item> adapter = new ArrayAdapter<Item>(this,
R.layout.list_item, item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Item item = (Item) parent.getItemAtPosition(pos);
Toast.makeText(parent.getContext(), item.getDetails(),
Toast.LENGTH_LONG).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
}
项目.java
public class Item {
private String name;
private int id;
private String category;
private String published;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public void setcategory(String category) {
this.category = category;
}
public String getcategory() {
return category;
}
public void setpublished(String published) {
this.published = published;
}
public String getpublished() {
return published;
}
public String getDetails() {
// TODO Auto-generated method stub
String result = id + ": " + name + "\n" + category + "-" + published;
return result;
}
}
SAXXMLParser.java
public class SAXXMLParser {
public static List<Item> parse(String URL) {
List<Item> menu=null;
try {
// create a XMLReader from SAXParser
XMLReader xmlReader = SAXParserFactory.newInstance().newSAXParser()
.getXMLReader();
// create a SAXXMLHandler
SAXXMLHandler saxHandler = new SAXXMLHandler();
// store handler in XMLReader
xmlReader.setContentHandler(saxHandler);
// the process starts
xmlReader.parse(URL);
// get the `Employee list`
menu = saxHandler.getmenu();
} catch (Exception ex) {
Log.d("XML", "SAXXMLParser: parse() failed");
}
// return Employee list
return menu;
}
}
SAXXMLHandler.java
public class SAXXMLHandler extends DefaultHandler {
private List<Item> menu;
private String tempVal;
private Item tempEmp;
public SAXXMLHandler() {
menu = new ArrayList<Item>();
}
public List<Item> getmenu() {
return menu;
}
// Event Handlers
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// reset
tempVal = "";
if (qName.equalsIgnoreCase("item")) {
// create a new instance of employee
tempEmp = new Item();
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
tempVal = new String(ch, start, length);
}
public void endElement(String uri, String localName, String qName)
throws SAXException {
if (qName.equalsIgnoreCase("item")) {
// add it to the list
menu.add(tempEmp);
} else if (qName.equalsIgnoreCase("id")) {
tempEmp.setId(Integer.parseInt(tempVal));
} else if (qName.equalsIgnoreCase("name")) {
tempEmp.setName(tempVal);
} else if (qName.equalsIgnoreCase("category")) {
tempEmp.setcategory(tempVal);
} else if (qName.equalsIgnoreCase("published")) {
tempEmp.setpublished(tempVal);
}
}
}
安卓宣言。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.xmlspinner"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.xmlspinner.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
日志猫详细信息
http://i.stack.imgur.com/BpNKV.jpg