0
votes

This method is binding the data on the list:

override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.bindItems(userList[position])
    holder.imgDelete.setOnClickListener(View.OnClickListener {
        Toast.makeText(this,"Delete Button Clicked", Toast.LENGTH_SHORT).show()

    })
    holder.imgCopy.setOnClickListener(View.OnClickListener {
        Toast.makeText(this,"Copy Button Clicked", Toast.LENGTH_SHORT).show()
    })
}

Error getting :

None of the following functions can be called with the arguments supplied: public open fun makeText(p0: Context!, p1: CharSequence!, p2: Int): Toast! defined in android.widget.Toast

please check and help

4

4 Answers

1
votes

use

Toast.makeText(<Your Activity Context>,"Copy Button Clicked", Toast.LENGTH_SHORT).show()

instead of

Toast.makeText(this,"Copy Button Clicked", Toast.LENGTH_SHORT).show()

How do you get context

Context context;

1-

@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
   super.onAttachedToRecyclerView(recyclerView);
   context = recyclerView.getContext();
}

2-

@Override
public CustomAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,int viewType) {
   context = parent.getContext();

   return YourViewHolder;
}

3-

holder.itemView.getContext() 

4-

holder.imgDelete.getContext()

5-

Pass activity context in constructor for CustomAdapter

1
votes

I think, the Application context, used here should be the activity class context, instead of this, because this inside an onClickListener, is actually not a applicationContext to the activity but that to the parent view.

Try this:

Toast.makeText(<Here_comes_your_activity_context>,"Copy Button Clicked", Toast.LENGTH_SHORT).show()

Dont forget to replace your activity context here, with your own.

0
votes

You don't have context in on click listener by just passing this. Get context from view instead.

Replace this to holder.imgDelete.context

 holder.imgDelete.setOnClickListener(View.OnClickListener {
        Toast.makeText(holder.imgDelete.context,"Delete Button Clicked", Toast.LENGTH_SHORT).show()
  })
0
votes

I would suggest a whole different approach.(BTW - I know you work in Kotlin, I'll share my code in java which I'm sure you'll understand but it will assist java coders as well).

Try saving a Context as a variable in your activity (in the on create set that variable to "this" of the activity. Make that variable a static variable. Like this:

public class MainActivity extands Activity{

    private static Context context;

    @Override
    protected void onCreate (Bundle savedInstanceState){
       //your code...
       context = this;
    }

    public static Context getContext(){
       return context;
    }
}

Then inside the Toast just do this:

override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
    holder.bindItems(userList[position])
    holder.imgDelete.setOnClickListener(View.OnClickListener {
        Toast.makeText(MainActivity.getContext(),"Delete Button Clicked", Toast.LENGTH_SHORT).show()

    })
    holder.imgCopy.setOnClickListener(View.OnClickListener {
        Toast.makeText(MainActivity.getContext(),"Copy Button Clicked", Toast.LENGTH_SHORT).show()
    })
}