我正在尝试使用 SaxParser 解析 xml 文件。我已经使用 url 和 xml 文件完成了此操作。但现在我想从一个字符串中解析它。
这是字符串:
String xml_string = "<?xml version="1.0" standalone="yes"?> <NewDataSet> <Table> <expnr>672 </expnr> <artnr>ML-824 </artnr> </Table> <Table> <expnr>672 </expnr> <artnr>ML-824 </artnr> </Table> </NewDataSet> "
现在我需要解析那个字符串,我试过这个:
InputStream stream = new ByteArrayInputStream(xml_string.getBytes());
items = SAXXMLParser.parse(stream);
但它不工作(在阵列适配器中出错)
这是错误:
3654-3654/E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:386)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2159)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
at android.widget.ListView.onMeasure(ListView.java:1158)
at android.view.View.measure(View.java:15518)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15518)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15518)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
at android.view.View.measure(View.java:15518)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:532)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.TextView
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:379)
... 36 more
解析这个的正确方法是什么?
编辑:我发现我做错了什么
我正在使用这段代码:
InputStream stream = new ByteArrayInputStream(resultFull.getBytes());
items = SAXXMLParser.parse(stream);
ArrayAdapter adapter= new ArrayAdapter(activty, R.layout.list, items);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
但我没有像这样使用我的 CustomListViewAdapter:
InputStream stream = new ByteArrayInputStream(resultFull.getBytes());
items = SAXXMLParser.parse(stream);
CustomListViewAdapter listViewAdapter = new CustomListViewAdapter(activty, items);
ListView lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(listViewAdapter);
并且使用最后一个代码它正在工作。这很愚蠢,但我找到了解决方案!谢谢您的帮助!