I have implemented a RecyclerView. Now I want to display an AlertDialog when an item of RecyclerView is clicked. I tried many ways and none of them worked. Here is my adapt
problem is here , you have to also pass context to the adapter constructor like given below
public SearchResultAdapter(List resultList , Context context) {
this.resultList = resultList;
this.context = context
}
in your Activity where you initiate adapter use like below
SearchResultAdapter madapter = new SearchResultAdapter (List, this);
then in your onBindViewHolder
@Override
public void onBindViewHolder(final MyViewHolder holder, int position) {
Searchresult result = resultList.get(position);
holder.item1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//pass the 'context' here
AlertDialog.Builder alertDialog = new AlertDialog.Builder(context);
alertDialog.setTitle("Your title");
alertDialog.setMessage("your message ");
alertDialog.setPositiveButton("CANCEL", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
alertDialog.setNegativeButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// DO SOMETHING HERE
}
});
AlertDialog dialog = alertDialog.create();
dialog.show();
}
});
}