如何在AlertDialog中放置图像?

时间:2017-01-24 08:49:05

标签: android listview alertdialog

我有一个显示图像列表的constructor(public navCtrl: NavController, private formBuilder: FormBuilder, private profileService: ProfileService, private loadingService: LoadingService) { this.formIsValid = true; //TODO: What??? Why is this happening? console.log(new Date("1997-1-1")); // null console.log(new Date("1999-12-31")); // "1999-12-31T00:00:00.000Z" console.log(new Date("2010-11-7")); // null console.log(new Date("1992-4-21")); // null console.log(new Date("1842-2-27")); // null console.log(new Date("2000-8-20")); // null this.dobForm = formBuilder.group({ dob: ['', Validators.required] }); } 。当我点击ListView中的图片时,我想在ListView中显示图片。

使用以下代码,我可以在AlertDialog中获取并显示TextView的文字形式。 但我不知道如何在AletDialog中显示图像。

我提到了许多消息来源。但没有任何帮助。

AlertDialog

我的问题是如何在点击listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { TextView c = (TextView) view.findViewById(R.id.textViewname); String value = c.getText().toString(); ImageView i = (ImageView) view.findViewById(R.id.imageView_temp); new AlertDialog.Builder(uploadpage.this). setTitle("title"). setMessage(value). show(); dialog.show(); } }); } 时显示(AlertDialogListVIew的特定图片? 我想显示单击的图像或项目。

修改

01-24 14:31:26.504 17180-17180/com.example.prakash.pix91 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.prakash.pix91, PID: 17180
                                                                           java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
                                                                               at android.support.v7.app.AlertDialog.resolveDialogTheme(AlertDialog.java:108)
                                                                               at android.support.v7.app.AlertDialog$Builder.<init>(AlertDialog.java:285)
                                                                               at com.example.prakash.pix91.uploadpage$6.onItemClick(uploadpage.java:507)
                                                                               at android.widget.AdapterView.performItemClick(AdapterView.java:310)
                                                                               at android.widget.AbsListView.performItemClick(AbsListView.java:1145)
                                                                               at android.widget.AbsListView$PerformClick.run(AbsListView.java:3073)
                                                                               at android.widget.AbsListView$3.run(AbsListView.java:3910)
                                                                               at android.os.Handler.handleCallback(Handler.java:746)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                               at android.os.Looper.loop(Looper.java:148)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

3 个答案:

答案 0 :(得分:2)

为您的图片创建一个布局文件,并将其创建到警报对话框

 AlertDialog.Builder   alertdialog = new AlertDialog.Builder(getActivity());
     LayoutInflater inflaterr = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View  viewtemplelayout= inflaterr.inflate(R.layout.imagefile, null);
      ImageView i = (ImageView) viewtemplelayout.findViewById(R.id.imageView_temp);//and set image to image view

         alertdialog.setView(viewtemplelayout);//add your view to alert dilaog
         alertdialog.show()

答案 1 :(得分:0)

您的AlertDialog的布局应包含ImageView以显示图像。实例化对话框时,您将从ListView传递图像视图。

ImageView image = new ImageView(getContext());
// Set the resource for the image view
// image.setBitmap(someImageBitmapFromListView);
// You can also set a drawable using setImageResource(Drawable drawable) on the ImageView
AlertDialog aDialog = new AlertDialog.Builder(getActivity())
        .setView(image)
        .setPositiveButton(android.R.string.ok,null)
        .setNegativeButton(android.R.string.cancel, null);
        .create();
aDialog.show();

从ListView中提取图像应该足够简单。如果您只需要显示图像,那么上面的代码应该可以帮助您入门。如果要显示更多内容,请查看创建自定义对话框的官方指南。

答案 2 :(得分:0)

You should create your custom layout like this.

AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.your_custom_layout, null);
dialogBuilder.setView(dialogView);

ImageView imageview= (ImageView) dialogView.findViewById(R.id.edittext);
imageview.setImageResource(R.id.image1);
AlertDialog alertDialog = dialogBuilder.create();
alertDialog.show();

That's it.