【问题标题】:Toast message is not working in Recycler ViewToast 消息在 Recycler 视图中不起作用
【发布时间】:2020-09-03 13:28:18
【问题描述】:

这个方法是绑定列表上的数据:

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()
    })
}

获取错误:

以下函数均不能使用参数调用 提供:public open fun makeText(p0: Context!, p1: CharSequence!, p2: Int): 干杯!在 android.widget.Toast 中定义

请检查并帮助

【问题讨论】:

    标签: android android-recyclerview android-toast


    【解决方案1】:

    我会建议一种完全不同的方法。(顺便说一句 - 我知道你在 Kotlin 工作,我将分享我的 Java 代码,我相信你会理解,但它也会帮助 Java 编码人员)。

    尝试将上下文保存为活动中的变量(在创建时将该变量设置为活动的“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;
        }
    }
    

    然后在 Toast 里面这样做:

    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()
        })
    }
    

    【讨论】:

      【解决方案2】:

      我认为,这里使用的应用程序上下文应该是活动类上下文,而不是this,因为在onClickListener 中的this 实际上不是活动的应用程序上下文,而是父视图的应用程序上下文。

      试试这个:

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

      不要忘记在此处用您自己的替换您的活动上下文。

      【讨论】:

      • 试过:Toast .makeText(CustomAdapter,"Delete Button Clicked", Toast.LENGTH_SHORT).show() //但不工作
      【解决方案3】:

      使用

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

      而不是

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

      你如何获得上下文

      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-

      在 CustomAdapter 的构造函数中传递活动上下文

      【讨论】:

      • 试过:Toast .makeText(CustomAdapter,"Delete Button Clicked", Toast.LENGTH_SHORT).show() //但不工作
      • 使用你的活动上下文而不是适配器。
      • 请告诉我如何在适配器类中添加活动。因为我的 onBindViewHolder 函数在 customAdapter 类中。
      • 试试这个 => holder.itemView.getContext()
      • @Rahul 您能否将答案标记为已接受的答案,这样没人会感到困惑......就像我一样。
      【解决方案4】:

      您没有通过传递这个来获得点击侦听器的上下文。而是从视图中获取上下文。

      this 替换为holder.imgDelete.context

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

      【讨论】:

        猜你喜欢
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-10-11
        • 2021-08-03
        • 2021-03-29
        相关资源
        最近更新 更多