我有这个 xml 文件
<Root>
<Item ItemNumber="1">
<nd ref='a' />
</Item>
<Item ItemNumber="2">
<nd ref='b' />
</Item> <Item ItemNumber="3">
<nd ref='c' />
</Item> <Item ItemNumber="4">
<nd ref='d' />
</Item> <Item ItemNumber="5">
<nd ref='e' />
</Item> <Item ItemNumber="6">
<nd ref='f' />
</Item>
</Root>
我想在列表视图中显示它但是当我这样做时 nd 节点进入另一行
例如
a
--
1
--
b
--
2
--
c
--
3
--
但我想要这样
a
1
--
b
2
--
c
3
--
我已经搜索过这个,但我无法得到它。你们能帮我解决这个问题吗?
这是 MainActivity.java
import java.io.IOException;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.view.Menu;
import android.widget.ArrayAdapter;
public class MainActivity extends ListActivity {
String item;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try{
item = getItemFromXML(this);
}catch (XmlPullParserException e){
}catch (IOException e){
}
String[] items = item.split("\n");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items));
}
public String getItemFromXML(Activity activity) throws XmlPullParserException, IOException{
StringBuffer stringBuffer = new StringBuffer();
Resources res = activity.getResources();
XmlResourceParser xpp = res.getXml(R.xml.items);
xpp.next();
int eventType = xpp.getEventType();
while (eventType != XmlPullParser.END_DOCUMENT){
if (eventType == XmlPullParser.START_TAG){
if (xpp.getName().equals("Item")){
stringBuffer.append(xpp.getAttributeValue(null, "ItemNumber") + "\n");
}
if (xpp.getName().equals("nd")){
stringBuffer.append(xpp.getAttributeValue(null, "ref") + "\n");
}
}
eventType = xpp.next();
}
return stringBuffer.toString();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(@android:id/list)
-->
<ListView
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>