简体   繁体   English

如何在AlertDialog框中将图像URI设置为图标?

[英]How can I set image URI as icon in AlertDialog box?

I want the uploaded image to be shown in my alert dialog box when an item is selected. 我希望当选择一个项目时在我的警报对话框中显示上载的图像。 This line of code show_builder.setIcon(imageView); 这行代码show_builder.setIcon(imageView); is not working for me. 对我不起作用。

Can you please tell me what am I missing in this code? 您能告诉我这段代码中我缺少什么吗? Thanks. 谢谢。

Here is a snippet of my code: 这是我的代码片段:

MainActivity.java MainActivity.java

@Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) {
        Student selectedStudents = studentArrayList.get(position);

        final Uri image = selectedStudents.getUriImage();
        final String lastname = selectedStudents.getStudlname();
        final String firstname = selectedStudents.getStudfname();
        final String course = selectedStudents.getStudcourse();
        final ImageView imageView = new ImageView(this);
        imageView.setImageURI(image);

        options_builder.setTitle("Choose an option");
        String [] options = {"Show", "Edit", "Delete"};

        options_builder.setItems(options, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                switch (which){
                    case 0:
                        show_builder.setTitle(""+lastname+", "+firstname+"\n"+course);
//                        show_builder.setIcon(imageView); //this line of code is not working
                        show_builder.setNeutralButton("Okay", null);

                        AlertDialog show_dialog = show_builder.show();
                        show_dialog.show();
                        break;
                    case 1:
                        Intent toedit = new Intent(MainActivity.this, EditStudentActivity.class);
//                        toedit.putExtra("student", studentArrayList.get(which));
                        startActivity(toedit);
                    case 2:
                        studentArrayList.remove(position);
                        adapter.notifyDataSetChanged();
                        Toast.makeText(getApplicationContext(), "Item removed!", Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });

        AlertDialog options_dialog = options_builder.show();
        options_dialog.show();

        return true;
    }

If you need to display an image from a url in an ImageView , I recommend you to use an image loading and caching libraries. 如果需要从ImageView的URL显示图像,建议您使用图像加载和缓存库。 Some well-known ones are Glide and Picasso . 一些著名的是GlidePicasso

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM