2

I want to open dialog box when i click on ImageView in Recyclerview.

When i show Toast it's working(Showing) but when i want to show AlertDialog Box it's not working (on image click application crash) Please solve it. This is my Adapter Class:-

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); } } 

}

3
  • what is your error? post your log Commented Jun 29, 2018 at 5:33
  • android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an applicationat com.example.hp.myapplication.RecyclerAdapter$1.onClick(RecyclerAdapter.java:65) Commented Jun 29, 2018 at 5:36
  • check this link: stackoverflow.com/questions/5796611/… Commented Jun 29, 2018 at 5:50

1 Answer 1

3

Try this code..

make interface into adapter class for click event handleing..

 OnItemClick onItemClick; public void setOnItemClick(OnItemClick onItemClick) { this.onItemClick = onItemClick; } public interface OnItemClick { void getPosition(); //pass any things } 

after that..

 @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(); } }); } 

after that make dialog code into one method and define into activity ..

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(); } 

after that bind adapter into recyclerview or listview it means adapter not null then call below code..

 adpater.setOnItemClick(new RecyclerViewAdpater.OnItemClick() { @Override public void getPosition() { showDialog(); } }); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank You So much Android Team, It's working.
then pl accept the answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.