在AlertDialog中显示列表 [英] Show list in AlertDialog

查看:769
本文介绍了在AlertDialog中显示列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am using an alert dialog that shows a menu on the beginning of the app, I want the dialog to show me 2 values which are "name"s from an object, here is the code of the alert dialog:

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是我的对象,其中包含字符串"name",而ApplicationController.entities包含数组

EntityType is my object that contain a string "name" and ApplicationController.entities contains the array

推荐答案

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();
}

这篇关于在AlertDialog中显示列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆