【问题标题】:Show list in AlertDialog [duplicate]在 AlertDialog 中显示列表 [重复]
【发布时间】:2016-07-31 11:03:44
【问题描述】:

我正在使用一个在应用程序开头显示菜单的警报对话框,我希望该对话框向我显示 2 个值,它们是来自对象的“名称”,这是警报对话框的代码:

public void showDialog() {

    EntityType en = new EntityType();
    ArrayList array = ApplicationController.entities;

    final AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setTitle("Pick one");
    builder.setItems(array, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            // I want to write my code here
        }
    });

    builder.show();
}

EntityType 是我的对象,其中包含字符串“名称”,ApplicationController.entities 包含数组

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    试试下面的代码……它对我有用……:)

    AlertDialog.Builder builderSingle = new AlertDialog.Builder(MainActivity.this);
        builderSingle.setIcon(R.drawable.green_tick_add);
        builderSingle.setTitle("Choose..");
    
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.select_dialog_item);
        arrayAdapter.add("Change Photo");
        arrayAdapter.add("Remove Photo");
    
        builderSingle.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
    
        builderSingle.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                String strName = arrayAdapter.getItem(which);
    
            }
        });
        builderSingle.show();
    

    【讨论】:

      【解决方案2】:
      public void showDialog() {
      
          EntityType en = new EntityType();
          ArrayList array = ApplicationController.entities;
      
          final AlertDialog.Builder builder = new AlertDialog.Builder(this);
          builder.setTitle("Pick one");
          ArrayList<String> displayValues=new ArrayList<>();
            for (Entity entity : array) {
               displayValues.add(entity.name);
            }
      
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_list_item_1,array);
            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Pick one");
            builder.setSingleChoiceItems(displayValues, 0, new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int which) {
                  Entity selectedItem = array[which];
               }
            });    
      
      
          builder.show();
      }
      

      【讨论】:

        【解决方案3】:
        ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.select_dialog);
        adapt.add("your entity name");
        

        然后编辑对话代码:

        builder.setAdapter(adapt, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int item) {
                    //write code here
                }
            });
        

        【讨论】:

          【解决方案4】:

          您必须创建一个 ArrayAdapter 来保存您的 EntityType 名称并将它们添加到其中:

          ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, android.R.layout.select_dialog_singlechoice);
          adapt.add("your entity name");
          

          然后在对话框中:

          builder.setAdapter(adapt, new DialogInterface.OnClickListener() {
                  public void onClick(DialogInterface dialog, int item) {
                      //Your code here
                  }
              });
          

          希望对你有帮助

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-10-11
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-01-05
            • 1970-01-01
            相关资源
            最近更新 更多