我有一个包含国家和国家代码的.xml文件。它看起来是这样的:
<?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);
发布于 2010-11-24 17:54:52
使用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>发布于 2010-11-24 17:48:20
您可能会扩展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参考资料非常好,你知道的。
发布于 2010-11-24 18:20:46
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()....
希望这能有所帮助。谢谢:)
https://stackoverflow.com/questions/4265261
复制相似问题