[英]How to set adapter for spinner in alert dialog
我正在制作Android应用程序,但发现了问题。 我有活动,因此必须从中进行警报对话框。 那个很好,但是我在该对话框中也需要微调器,并且我无法正确设置适配器。 NullPointer ex导致应用程序崩溃。 使用微调器的对话框xml:
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:drawSelectorOnTop="true" />
这是代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_list);
MyListView = (ListView) findViewById(android.R.id.list);
registerForContextMenu(MyListView);
myList = getIntent().getParcelableArrayListExtra(
MainActivity.NUMBER_LIST);
for (MyCallLog m : myList) {
stringList.add(m.getNumber());
}
MyListView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, stringList) {
});
}
和对话框代码:
public void DialogData(final int DialogTyp) {
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.dialog, null);
AlertDialog.Builder MyBuilder = new AlertDialog.Builder(this);
MyBuilder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (DialogTyp == 1) {
}
else if (DialogTyp == 2) {
stringList.remove(aktual);
}
MyListView.invalidateViews();
return;
}
});
MyBuilder.setNegativeButton("Storno",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
return;
}
});
if (DialogTyp != 2)
MyBuilder.setView(layout);
AlertDialog MyAlertDialog = MyBuilder.create();
if (DialogTyp == 0)
MyAlertDialog.setTitle("Nový záznam");
else if (DialogTyp == 1) {
addItemsOnSpinner();
MyAlertDialog.setTitle("Editace záznamu");
((EditText)layout.findViewById(R.id.cislo)).setText(myCallLog.getNumber());
((EditText)layout.findViewById(R.id.delka)).setText(String.valueOf(myCallLog.getDuration()));
((EditText)layout.findViewById(R.id.datum)).setText("25.2.2015");
((Spinner)layout.findViewById(R.id.spinner)).setSelection(getSelectedOperator(myCallLog.getOperator()));
} else if (DialogTyp == 2) {
MyAlertDialog.setTitle("Smazání záznamu" + aktual);
MyAlertDialog.setMessage("Opravdu chcete smazat vybraný záznam?");
}
MyAlertDialog.show();
}
public void editList(int pozition, String number, String date, String spinner){
}
public void addItemsOnSpinner() {
List<String> list = new ArrayList<String>();
list.add("O2");
list.add("T_MOBILE");
list.add("VODAFONE");
spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this, android.R.layout.simple_spinner_item, list);
spinner.setAdapter(adapter);
}
在最后一行,这里应该有错误。 问题出在哪里或者我必须做些什么才能正确设置它?
spinner
为null,因为您正在尝试从Activity(不是对话框布局)中findViewById
。 而是通过您在此处检索到的微调器
((Spinner)layout.findViewById(R.id.spinner))
放入您的public void addItemsOnSpinner() {...}
方法中。
您正在尝试从其他视图获取Spinner,如果Spinner在对话框中,则应该执行以下操作:
AlertDialog MyAlertDialog = MyBuilder.create();
Spinner spinnerInsideDialog = (Spinner) MyAlertDialog.findViewById(R.id.spinner);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.