我正在尝试使用其中的微调器创建一个alertdialog.builder。微调器项最初是从数据库中填充的。这是我的代码的一部分:
update.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
final View update_layout = getLayoutInflater().inflate(R.layout.update_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(update_layout);
builder.setTitle("Update existing DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which)
{
spinner = (Spinner) update_layout.findViewById(R.id.spinner1);
List<String> arrayspin=new ArrayList<String>();
Cursor csr1 = db.rawQuery("SELECT name FROM sqlite_master WHERE type='table'", null);
int count = csr1.getCount();
csr1.moveToFirst();
for (int m=0; m>count; m++){
if (csr1.getString(csr1.getColumnIndex("name"))!="android_metadata")
{
String cont = csr1.getString(csr1.getColumnIndex("name"));
arrayspin.add(cont);
}
csr1.moveToNext();
}
ArrayAdapter adp = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,arrayspin);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
table2 = spinner.getSelectedItem().toString();
editText2 = (EditText) update_layout.findViewById(R.id.editText2);
new Updating().execute();
}
});
builder.setNegativeButton("back",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
它没有给我任何错误,但在应用程序中,微调器没有显示任何项目。然后我尝试修改代码,只是为了测试微调器,就像这样:
update.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
final View update_layout = getLayoutInflater().inflate(R.layout.update_layout, null);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setView(update_layout);
builder.setTitle("Update existing DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
spinner = (Spinner) update_layout.findViewById(R.id.spinner1);
String[] items = { "this", "is", "a", "really","really2", "really3",
"really4", "really5", "silly", "list" };
ArrayAdapter adp = new ArrayAdapter(MainActivity.this,android.R.layout.simple_spinner_item,items);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adp);
table2 = spinner.getSelectedItem().toString();
editText2 = (EditText) update_layout.findViewById(R.id.editText2);
new Updating().execute();
}
});
builder.setNegativeButton("back",new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
dialog.dismiss();
}});
AlertDialog alert = builder.create();
alert.show();
}});
但是微调器仍然没有显示任何物品。我做错了什么?
答案 0 :(得分:2)
点击“Alert Dialog”按钮,即可填充微调器。您应该将代码移到onClickListener
之外final View update_layout = getLayoutInflater().inflate(
R.layout.update_layout, null);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Update existing DB");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
Spinner spinner = (Spinner) update_layout.findViewById(R.id.spinner1);
String[] items = { "this", "is", "a", "really", "really2",
"really3", "really4", "really5", "silly", "list" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(HomeScreen.this,
android.R.layout.simple_spinner_dropdown_item, items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
builder.setView(update_layout);
AlertDialog alert = builder.create();
alert.show();