I\'m using RecyclerView
to list some text and now I want to make it so that when the user clicks on text a custom Alert Dialog box pops up.
I have tried
final Dialog dialog = new Dialog(your_activity_context);
Write this code:
final Dialog dialog = new Dialog(CBAdapter.this);
instead of
final Dialog dialog = new Dialog(context);
(or)
context = CBAdapter.this; // Initialize context
Hope this helps.
Happy Coding :)
You are using context
which is null so pass the context
in ViewHolder
constructor and in CBAdapter constructor also like as below:
public class CBAdapter extends RecyclerView.Adapter<CBAdapter.ViewHolder> {
List<AdapterData> mItems;
Context context;
public CBAdapter(Context context) {
super();
this.context = context;
.....
}
And in ViewHolder class
class ViewHolder extends RecyclerView.ViewHolder{
public TextView textOne;
private Context mcontext;
public ViewHolder(View itemView, Context mcontext) {
super(itemView);
this.mcontext = mcontext;
....
}
Not directly related to the question, although I beg you: DO NOT set onClickListener-s inside adapter!
This is how it should be done:
private class ItemDataHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener{
private final String TAG = ItemDataHolder.class.getSimpleName();
/**
* Define view's elements
*/
/**
* Define object instance
*/
private Item mData;
// Constructor
public MessageDataHolder(View itemView) {
super(itemView);
/**
* Init elements
*/
itemView.setOnLongClickListener(this);
}
/**
* Method to handle long click on the item
* @param v View to handle click on
* @return
*/
@Override
public boolean onLongClick(View v) {
Log.v(TAG, "Long click fired!");
return false;
}
/**
* Function to update view's elements
* @param message Good data to be updated to
*/
public void bindData(Item message) {
mData = message;
/**
* Set values of views here
**/
}
}
Hope my answers helps someone to write a better code :)
Nevermind I forgot the initialization my context
context = itemView.getContext();
This is not the answer for your query but the better way to handle this scenario.
Use callback methods.
In your Activity:
This will implement the interface that we have in our Adapter
. In this example, it will be called when the user clicks on an item in the RecyclerView
.
public class MyActivity extends Activity implements AdapterCallback {
private MyAdapter mMyAdapter;
@Override
public void onMethodCallback() {
// Show your alert
}
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.mMyAdapter = new MyAdapter(this);
}
}
In your Adapter:
In the Activity, we initiated our Adapter
and passed this as an argument to the constructor. This will initiate our interface for our callback method. You can see that we use our callback method for user clicks.
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
private AdapterCallback mAdapterCallback;
public MyAdapter(Context context) {
try {
this.mAdapterCallback = ((AdapterCallback) context);
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement AdapterCallback.");
}
}
@Override
public void onBindViewHolder(final MyAdapter.ViewHolder viewHolder, final int i) {
// simple example, call interface here
// not complete
viewHolder.itemView.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
mAdapterCallback.onMethodCallback();
} catch (ClassCastException exception) {
// do something
}
}
});
}
public static interface AdapterCallback {
void onMethodCallback();
}
}
Courtesy : Call Activity method from adapter