繁体   English   中英

来自适配器 Class 的访问意图

[英]Access Intent from Adapter Class

我正在尝试获取适配器 class 以获取存储在另一个适配器 class 中的意图。 这个想法是第一个适配器 class 将存储一个意图并启动第二个适配器 class(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);
                }
            });
        }
    }

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

检查你的身份证? 它是空的吗:还有一些指导方针。 在 recyclerview、adapter 中创建一个接口,在您的父活动(适配器放置的位置)中实现该接口。 然后将您的活动实例作为在适配器内部创建的接口传递,并在 onclick 侦听器内部使用它。 这个想法是将意图的逻辑传递给活动:接口:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM