0

我正在尝试制作一个带有微调器的 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();
  }});

但微调器仍然没有显示任何项目。我做错了什么?

4

1 回答 1

2

You are populating your spinner on click of Alert Dialog positive button. You should move the code outside of the 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();
于 2013-05-04T05:53:07.000 回答