解析XML并将其显示在Android中的AlertDialog中
我有这个带有国家及其国家代码的.xml文件。这是它的样子:解析XML并将其显示在Android中的AlertDialog中
<?xml version="1.0" encoding="UTF-8"?>
<landen>
<land>
<naam>Afghanistan</naam>
<code>AF</code>
</land>
<land>
<naam>Albani�</naam>
<code>AL</code>
</land>
<land>
<naam>Algerije</naam>
<code>DZ</code>
</land>
<land>
</landen>
现在我想要人们从列表中选择一个国家。我虽然AlertDialog会很好显示一切。
我得到的值超出我的XML文件的方式是这样的:
protected ArrayList<Land> getLanden() {
ArrayList<Land> lijst = new ArrayList<Land>();
try {
DocumentBuilder builder =DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(getAssets().open("landenlijst.xml"));
NodeList nl = doc.getElementsByTagName("land");
for (int i=0;i<nl.getLength();i++) {
Node node = nl.item(i);
Land land = new Land();
land.land = Xml.innerHtml(Xml.getChildByTagName(node, "naam"));
land.landcode = Xml.innerHtml(Xml.getChildByTagName(node, "code"));
lijst.add(land);
}
Log.d("Gabug","Klaar met parsen");
Log.d("Gabug","Landen: " + lijst);
} catch (Exception e) {
e.printStackTrace();
}
return lijst;
}
而且我用这个做我的AlertDialog:
public void KiesLandMenu(){
ArrayList<Land> alleLanden = getLanden();
final CharSequence[] items = alleLanden.toArray(new CharSequence[alleLanden.size()]);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Kies land");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
switch (item){
case 0:
break;
case 1:
break;
case 2:
break;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
我不知道,如果这作为DDMS返回一些字节码或当我登录它的东西。之后,它因为ArrayStoreException而强制关闭。
现在我的问题是;这是做这件事的最好方法吗?如果是的话,我该如何解决ArrayStoreException?如果不是,有什么更好的方式让我的用户选择一个国家(也许是一种全新的观点)? 此外,我怎样才能注册某个国家的人窃听?
编辑:
我稍微改变以下示例代码,现在我得到一个NullPointerException异常..
public void KiesLandMenu(){
ArrayAdapter<Land> arrAdapter;
ArrayList<Land> alleLanden = getLanden();
arrAdapter = new ArrayAdapter<Land>(this, android.R.layout.simple_list_item_single_choice, alleLanden);
ListView list = (ListView)findViewById(R.layout.lijstview);
list.setAdapter(arrAdapter);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Log.e("item clicked", String.valueOf(position));
}
});
}
的NullPointerException异常是在list.setAdapter(arrAdapter);
使用ListView进行布局,然后在onCreate中设置该布局。做出的排行榜,你可以这样做:
public class RunTestProject extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); //whatever you want your layout to be
}
// getLanden() implementation goes here
public void KiesLandMenu(){
ArrayList<Land> alleLanden = getLanden();
arrAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, alleLanden);
Dialog dialog = new Dialog(this);
dialog.setTitle("Kies land");
dialog.setContentView(R.layout.withList); // The dialog layout
ListView list = (ListView) dialog.findViewById(android.R.id.list); //note that it's not simply findViewById
list.setAdapter(arrAdapter);
list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> list, View view, int position,
long id) {
Log.e("item clicked", String.valueOf(position));
}
});
dialog.show();
}
}
当用户选择一个项目,你可以将这个项目在数组中的位置显示在日志中看到的。
您的布局文件可以是这样的:
<?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"
>
<ListView android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
/>
</LinearLayout>
你也许可以延长AlertDialog
,并给它一个ListView
作为一个视图。然后将ListView
绑定到使用您的ArrayList
的ListAdapter
。
编辑:
ListView lv = new ListView(context);
ArrayAdapter aa = new ListAdapter(context, viewid, lijst);
lv.setAdapter(aa);
AlertDialog ad = new AlertDialog(context);
ad.setView(lv);
有比虽然更多的工作。您需要指定viewid
这是代表ListView
中每个项目的View
。
sdk参考非常好,你知道。
new AlertDialog.Builder(this)
.setIcon(R.drawable.alert_dialog_icon)
.setTitle(R.string.alert_dialog_single_choice)
.setSingleChoiceItems(<ListAdapter> or CharaSequnce[] , 0, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked on a radio button do some stuff */
}
})
.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked Yes so do some stuff */
}
})
.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
/* User clicked No so do some stuff */
}
})
.create();
注: 请参阅This link to Api下面
.setSingleChoiceItems提到粗体字(CharacterSequnce [],0,新的DialogInterface.OnClickListener()....
希望这会有所帮助。谢谢:)
(ListView)findViewById(R.layout.lijstview)返回null。这是R.id.lijstview。 – techiServices 2010-11-24 10:29:07
我在layout文件夹中创建了lijstview.xml,它在Eclipse中不会给我一个错误。当我把R.id.lijstview放在那里时,它会给我一个错误。 – Galip 2010-11-24 10:40:03
你应该先在onCreate中设置你的布局。我将在下面编辑我的答案。 – Zarah 2010-11-24 11:13:39