简体   繁体   English

来自适配器 Class 的访问意图

[英]Access Intent from Adapter Class

I am trying to get an adapter class to get the intent that was stored in another adapter class.我正在尝试获取适配器 class 以获取存储在另一个适配器 class 中的意图。 The idea is that the first adapter class will store an intent and start the activity of the second adapter class (Player_Adapter) as shown below:这个想法是第一个适配器 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);
                }
            });
        }
    }

My problem is that I cannot use the getIntent() function from my second adapter class.我的问题是我无法使用我的第二个适配器 class 中的 getIntent() function。 I have noticed that I was able to use the function in another class that extended "AppCompatActivity".我注意到我能够在另一个扩展“AppCompatActivity”的 class 中使用 function。 I have also read from another thread that the function is not a part of the Adapter class.我还从另一个线程中了解到 function 不是适配器 class 的一部分。 Is there any way around this?有没有办法解决? I am aware that I can just have one adapter class for multiple activity but first I want to resolve this issue, thank you.我知道我只能使用一个适配器 class 进行多项活动,但首先我想解决这个问题,谢谢。

Check your Id?检查你的身份证? Is it empty: And little guidlines.它是空的吗:还有一些指导方针。 Create an interface inside of recyclerview,adapter, implement that interface inside of your parent activity(where your adapter placed).在 recyclerview、adapter 中创建一个接口,在您的父活动(适配器放置的位置)中实现该接口。 then pass your activity instance as created interface inside of adapter and use it inside of onclick listener.然后将您的活动实例作为在适配器内部创建的接口传递,并在 onclick 侦听器内部使用它。 The idea is to pass logic of intent to activity: Interface:这个想法是将意图的逻辑传递给活动:接口:

public interface TeamClicker{
void onTeamClicked(int teamId);
}
  1. Create it on top of your adapter.在适配器之上创建它。
  2. Create interface variable.创建接口变量。
  3. Implement that interface in your activity.在您的活动中实现该接口。
  4. Pass instance of your activity inside of adapter as interface and put it put to your created variable.将适配器内部的活动实例作为接口传递,并将其放入您创建的变量中。
  5. Use that interface inside of onclickListener.在 onclickListener 中使用该接口。

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

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