微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

来自适配器类的访问意图

如何解决来自适配器类的访问意图

我正在尝试获取适配器类以获取存储在另一个适配器类中的意图。这个想法是,第一个适配器类将存储一个意图并启动第二个适配器类(Player_Adapter)的活动,如下所示:

public static class NBAViewHolder extends RecyclerView.ViewHolder {
        public LinearLayout containerView;
        public TextView textView;

        NBAViewHolder(View view) {

            super(view);

            containerView = view.findViewById(R.id.nba_row);
            textView = view.findViewById(R.id.nba_row_text_view);

            containerView.setonClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) { //on clicking the TEAMS will transfer the url to the "TEAMSActivity" class
                    TEAMS current = (TEAMS) containerView.getTag();
                    //an intent is a "glue" between activities,connecting them. He`re the intent is transferring the
                    //url that contains the properties of the TEAMS to the class "TEAMSActivity".
                    //can possibly create a new adapter class
                    Intent intent = new Intent(v.getContext(),com.example.player.Player_Adapter.class);
                    //we get the "fullName"
                    intent.putExtra("id",current.getId());
                    v.getContext().startActivity(intent);
                }
            });
        }
    }

我的问题是我无法使用第二个适配器类中的getIntent()函数。我注意到我能够在扩展“ AppCompatActivity”的另一个类中使用该函数。我还从另一个线程中读取到该函数不是Adapter类的一部分。有没有办法解决?我知道我只能为多个活动使用一个适配器类,但是首先,我想解决这个问题,谢谢。

解决方法

检查您的ID!是空的吗?和一些指导原则: 在recyclerview.adapter内创建一个接口,在父活动(适配器放置的位置)内实现该接口,然后将活动实例作为已创建的接口传递到适配器内,并在onclick侦听器内使用它。这个想法是将意图逻辑传递给活动。 界面:

public interface TeamClicker{
void onTeamClicked(int teamId);
}
  1. 在适配器顶部创建它。
  2. 创建接口变量。
  3. 在您的活动中实现该界面。
  4. 将您的活动实例作为接口传递给适配器,并将其放入您创建的变量中。
  5. 在onclickListener中使用该接口。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。