-6

I want to open a dialog in the onClick event of the items listed in a RecyclerView.

This is the Activity code:

public class EventPage extends AppCompatActivity {
EventAdapter adapter;
private RecyclerView recyclerView;
ArrayList<String> ticketName=new ArrayList<String>();
ArrayList<String>ticketPrice=new ArrayList<String>();
ArrayList<String>ticketCode=new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.eventfinished);

    recyclerView = (RecyclerView) findViewById(R.id.rvUserProfile);
    adapter = new EventAdapter(ticketName, ticketPrice,getApplicationContext(),recyclerView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setAdapter(adapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    adapter.notifyDataSetChanged();

This is the Adapter code:

public class EventAdapter extends RecyclerView.Adapter<info.androidhive.uplus.EventAdapter.MyViewHolder> {
private ArrayList<String> mDataset=new ArrayList<String>();
private ArrayList<String> mPrice=new ArrayList<String>();
Context ctx;
RecyclerView rec;
Uri uri;

public EventAdapter(ArrayList<String> mDataset,ArrayList<String> mPrice ,Context ctx,RecyclerView rec){
    this.mDataset   = mDataset;
    this.mPrice     = mPrice;
    this.ctx        = ctx;
    this.rec        =rec;
}

public static class MyViewHolder extends RecyclerView.ViewHolder{
    // public CardView mCardView;
    public TextView mTextView, mTicketPrice;
    public RelativeLayout relativeLayout;// mCardView;
    public MyViewHolder(View v, final Context context,final ArrayList<String>eventName, final ArrayList<String>eventPrice,final RecyclerView rcc){
        super(v);
        mTextView       = (TextView) v.findViewById(R.id.ticketName1);
        mTicketPrice    = (TextView) v.findViewById(R.id.ticketamount);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int p=rcc.indexOfChild(v);
                try
                {
                    final Dialog dialog = new Dialog(context);
                    dialog.setCancelable(true);
                    dialog.setContentView(R.layout.bookticket);
                    dialog.show();
                    Toast.makeText(context,"Clicked"+eventName.get(p),Toast.LENGTH_LONG).show();
                }
                catch (Exception ex)
                {
                    ex.printStackTrace();
                }
            }
        });
    }
}


@Override
public info.androidhive.uplus.EventAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.eventitem, parent, false);
    info.androidhive.uplus.EventAdapter.MyViewHolder vh = new info.androidhive.uplus.EventAdapter.MyViewHolder(v, this.ctx, this.mDataset, this.mPrice, this.rec);
    return vh;
}

@Override
public void onBindViewHolder(final info.androidhive.uplus.EventAdapter.MyViewHolder holder, final int position){
    holder.mTextView.setText(mDataset.get(position));
    holder.mTicketPrice.setText(currencyConverter(mPrice.get(position)));

}

This is the error that I am getting when clicking the element in the list:

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
croxy
  • 3,641
  • 8
  • 25
  • 42

1 Answers1

0

your context is null,Follow this below step to bring context in java class

 public static Context contextOfApplication;  // create variable

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

         contextOfApplication = getApplicationContext();
}

Create method outside Oncreate

public static Context getContextOfApplication(){
        return contextOfApplication;
    }

Now in your adapter class add the context

final Dialog dialog = new Dialog("youractivityname".getContextOfApplication());

(OR) @SachinVarma suggestion

simply Use this v.getContext()

Gowthaman M
  • 6,996
  • 7
  • 27
  • 51
  • 2
    Couldn't they use parent.getContext() in onCreateViewHolder ? – Lance Toth Feb 22 '18 at 11:02
  • for the activity yes the getApplicationContext() works but how does it work in the recycle-view adapter. – Clement Muhirwa Feb 22 '18 at 11:07
  • @LanceToth yes we can use.... – Gowthaman M Feb 22 '18 at 11:09
  • @ClementMuhirwa please check my code carefully......i am bring context from Activity to Recylierview adapter.. – Gowthaman M Feb 22 '18 at 11:10
  • he can simply take v.getContext() no need to pass it from Activity. eg final Dialog dialog = new Dialog(v.getContext());, i don't see any need of passing it from activity. – Sachin Varma Feb 22 '18 at 11:26
  • @SachinVarma Thank you for your suggestion.i Will update my answer..But For normal java class v.getContext willnot work.... – Gowthaman M Feb 22 '18 at 11:30
  • normal java classes in the scene? you can use it activity also, but not needed because you have direct access to the context. – Sachin Varma Feb 22 '18 at 11:36
  • 2
    Thanks @SachinVarma it works i only had to pass v.getCOntext() thank you so much – Clement Muhirwa Feb 22 '18 at 15:48
  • @SachinVarma cool !!!!. i mean to say For example: **class A{ public void print(){ // For example here i want print some toast } }** How you will do...you can achieve by using contractor or my above logiss... – Gowthaman M Feb 22 '18 at 18:02
  • 1
    @GowthamanM for a simple class A pass the context but make sure to remove it when you are done with class A i.e. context=null after you are finished. Also, NEVER pass context as statics. It will cause memory leaks, which is one thing but static sometimes become NULL in case of memory shortage on phone. Pass the context via constructor in such case as mentioned earlier. – Sachin Varma Feb 23 '18 at 04:33
  • @SachinVarma Noted thank you for your suggestions...but static variable will not create more memory..because static variable stored in heap whenever we assign value for static variable it will just the value in the same memory....it willnot create new memory again then how memory leak will heap. – Gowthaman M Feb 23 '18 at 05:19
  • @GowthamanM Can't explain it like this, read https://dzone.com/articles/why-static-bad-and-how-avoid, https://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil, http://blog.vogella.com/2013/03/12/android-why-not-to-use-static-for-view-fields-in-your-activity-or-fragments-by-lars-vogel/ – Sachin Varma Feb 23 '18 at 05:21
  • @SachinVarma hahaha cool..Thank you – Gowthaman M Feb 23 '18 at 05:25