我正在用相机拍照,并将它们作为位图添加到 ArrayList 中。我有 recyclerView 显示拍摄的图像。我想要一个警报对话框,显示单击的图像以及删除选项。我在将自定义布局设置为 AlertDialog 时遇到问题。当我单击图像时,应用程序崩溃并显示错误
java.lang.ClassCastException: android.support.v7.widget.AppCompatImageView cannot be cast to android.view.ViewGroup
错误指向这行代码
View viewInflated = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_image, (ViewGroup) view,false); imageDialog.setView(viewInflated);
但我不知道我应该做什么。我尝试将 (ViewGroup) 更改为其他内容,但没有成功。
适配器.java
public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {
private List<Bitmap> fotky;
private Bitmap foto;
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_item, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
foto = fotky.get(position);
holder.photo.setImageBitmap(foto);
holder.photo.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder imageDialog = new AlertDialog.Builder(view.getContext());
imageDialog.setTitle("Image");
View viewInflated = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_image, (ViewGroup) view,false);
imageDialog.setView(viewInflated);
ImageView image = viewInflated.findViewById(R.id.fotka);
image.setImageBitmap(foto);
imageDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.cancel();
}
});
imageDialog.setNegativeButton("delete", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
remove(position);
}
});
imageDialog.show();
//TODO tady ukázat full image s možností smazání
//remove(position);
}
});
}
public void add(int position, Bitmap item) {
fotky.add(position, item);
notifyItemInserted(position);
}
public void remove(int position) {
fotky.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, fotky.size());
}
public Adapter(List<Bitmap> myDataset) {
fotky = myDataset;
}
@Override
public int getItemCount() {
return fotky.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public ImageView photo;
public ViewHolder(View itemView) {
super(itemView);
photo = itemView.findViewById(R.id.imageView);
}
}
}
Dialog_image.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fotka"
android:contentDescription="photo"/>
</LinearLayout>
最佳答案
此崩溃背后的原因是您在 holder.photo
上设置了 AppCompatImageView
类型的点击监听器。在 onClick block 内,您将其类型转换为 ViewGroup
。
将其更改为以下。
View viewInflated = LayoutInflater.from(view.getContext()).inflate(R.layout.dialog_image, null);
应该可以。
关于java - android.support.v7.widget.AppCompatImageView 无法转换为 android.view.ViewGroup,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47379462/