Custom Alert Dialog Using RecyclerView

I use RecyclerView to enumerate some text, and now I want to make a custom warning dialog box pop up when the user clicks on the text.

I have tried this so far, but get a NullPointerException; what could be wrong here?

 public class CBAdapter extends RecyclerView.Adapter<CBAdapter.ViewHolder> { List<AdapterData> mItems; public CBAdapter() { super(); mItems = new ArrayList<>(); AdapterData data = new AdapterData(); data.setTextOne("Many Bows"); mItems.add(data); data = new AdapterData(); data.setTextOne("Pardon"); mItems.add(data); data = new AdapterData(); data.setTextOne("Fall To Knees & Beg"); mItems.add(data); data = new AdapterData(); data.setTextOne("Backflips"); mItems.add(data); } @Override public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) { View v = LayoutInflater.from(viewGroup.getContext()) .inflate(R.layout.test3, viewGroup, false); return new ViewHolder(v); } @Override public void onBindViewHolder(ViewHolder viewHolder, int i) { AdapterData data = mItems.get(i); viewHolder.textOne.setText(data.getTextOne()); } @Override public int getItemCount() { return mItems.size(); } class ViewHolder extends RecyclerView.ViewHolder{ public TextView textOne; private Context context; public ViewHolder(View itemView) { super(itemView); textOne = (TextView)itemView.findViewById(R.id.textView1); itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { final Dialog dialog = new Dialog(context); dialog.setContentView(R.layout.custom_dialog); dialog.setTitle("Title"); TextView text = (TextView) dialog.findViewById(R.id.text); text.setText("hello world"); ImageView image = (ImageView) dialog.findViewById(R.id.image); image.setImageResource(R.drawable.ic_launcher); Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK); dialogButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { dialog.dismiss(); } }); dialog.show(); } }); } } } 
+8
android android-alertdialog android-recyclerview
source share
6 answers

Nevermind I forgot the initialization of my context

context = itemView.getContext ();

+4
source share

This is not an answer to your request, but the best 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 Activity, we initiated our Adapter and passed this as an argument to the constructor. This will launch our interface for our callback method. You can see that we use our callback method for custom 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(); } } 

Provided: Invoking an activity method from the adapter

+3
source share
 final Dialog dialog = new Dialog(your_activity_context); 
+2
source share

You are using context , which is null , so pass the context constructor to the ViewHolder and the CBAdapter constructor, as shown below:

 public class CBAdapter extends RecyclerView.Adapter<CBAdapter.ViewHolder> { List<AdapterData> mItems; Context context; public CBAdapter(Context context) { super(); this.context = context; ..... } 

And in the ViewHolder class

 class ViewHolder extends RecyclerView.ViewHolder{ public TextView textOne; private Context mcontext; public ViewHolder(View itemView, Context mcontext) { super(itemView); this.mcontext = mcontext; .... } 
0
source share

It is not directly related to the issue, although I ask you: DO NOT install the onClickListener internal adapter!

Here's how it should be done:

 private class ItemDataHolder extends RecyclerView.ViewHolder implements View.OnLongClickListener{ private final String TAG = ItemDataHolder.class.getSimpleName(); /** * Define view 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 elements * @param message Good data to be updated to */ public void bindData(Item message) { mData = message; /** * Set values of views here **/ } } 

Hope my answers help someone write better code :)

0
source share

Enter this code:

  final Dialog dialog = new Dialog(CBAdapter.this); 

instead

 final Dialog dialog = new Dialog(context); 

(or)

 context = CBAdapter.this; // Initialize context 

Hope this helps.

Happy coding :)

-2
source share

All Articles