如何将数据从活动发送到AlertDialog以显示它们

时间:2013-10-08 10:04:15

标签: android dialog alertdialog

创建构建器的代码:

    builder = new AlertDialog.Builder(this);
    builder.setPositiveButton("connect",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int     id) {
                    devices.get(currentPos).setConnected(true);
                }
            });
    builder.setNegativeButton("dismiss",
            new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {
                    // User cancelled the dialog
                }
            });
    builder.setView(getLayoutInflater().inflate(
            (R.layout.activity_device_details), null));
    builder.setTitle("more information");

注意:builder.setView()到R.layout.activity_device_details,当我使用这段代码创建对话框时,我想要填充一些TextView:

    BPDevice dev = new BPDevice();
    dev = devices.get(position);

    AlertDialog dialog = builder.create();

    ((TextView) dialog.findViewById(R.id.name)).setText(dev.getName());

    dialog.show();

由于这一行,我得到一个nullpointerException:((TextView)dialog.findViewById(R.id.name))。setText(dev.getName());

¿如何正确填写TextView?

1 个答案:

答案 0 :(得分:1)

首先为您提供文本视图的根布局。然后你的孩子观看。然后设置数据并将root设置为警报对话框的视图。

View root =getLayoutInflater().inflate(
        (R.layout.activity_device_details),null);

 TextView textView =(TextView)root.findViewById(R.id.your_textview_name);
  textView.setText("Your data");

builder.setView(根);