当我在Recyclerview中单击ImageView时,我想打开对话框。
当我显示Toast时,它正在运行(正在显示),但是当我想要显示时 AlertDialog Box不起作用(单击图像时应用程序崩溃),请解决它。 这是我的适配器类:-
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.MyViewHolder> {
private ArrayList<GetterSetter> arrayList;
private Context context;
public RecyclerAdapter(ArrayList<GetterSetter> arrayList, Context context){
this.arrayList = arrayList;
this.context = context;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.detail,viewGroup,false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {
GetterSetter fp = arrayList.get(i);
myViewHolder.nametxt.setText(fp.getEmpname());
myViewHolder.companytxt.setText(fp.getCompanyname());
myViewHolder.proImg.setImageResource(fp.getEmppic());
myViewHolder.callImg.setImageResource(fp.callpic);
myViewHolder.msgimg.setImageResource(fp.msgpic);
myViewHolder.callImg.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.design);
dialog.setTitle("Dialog Box");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
Button button = (Button) dialog.findViewById(R.id.okbtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
@Override
public int getItemCount() {
return arrayList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
ImageView proImg,callImg,msgimg;
TextView nametxt,companytxt;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
itemView.setOnClickListener(null);
proImg = itemView.findViewById(R.id.imageView);
callImg = itemView.findViewById(R.id.smallimage1);
msgimg = itemView.findViewById(R.id.smallimage2);
nametxt = itemView.findViewById(R.id.name);
companytxt = itemView.findViewById(R.id.company);
}
}
}
答案 0 :(得分:0)
尝试此代码。
使接口成为适配器类,以进行单击事件处理。
OnItemClick onItemClick;
public void setOnItemClick(OnItemClick onItemClick) {
this.onItemClick = onItemClick;
}
public interface OnItemClick {
void getPosition(); //pass any things
}
之后。
@Override
public void onBindViewHolder(ItemViewHolder holder, final int position) {
// below code handle click event on recycler view item.
String data = mStringList.get(position);
holder.textView.setText(data);
holder.imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
onItemClick.getPosition();
}
});
}
然后将对话代码变成一种方法并定义为活动..
private void showDialog(){
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.design);
dialog.setTitle("Dialog Box");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
Button button = (Button) dialog.findViewById(R.id.okbtn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
dialog.dismiss();
}
});
dialog.show();
}
在将适配器绑定到recyclerview或listview之后,这意味着适配器不为null,然后调用以下代码。
adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() {
@Override
public void getPosition() {
showDialog();
}
});