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 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();
}
});
}
}
}
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
final Dialog dialog = new Dialog(your_activity_context);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With