简体   繁体   中英

Access Intent from Adapter Class

I am trying to get an adapter class to get the intent that was stored in another adapter 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:

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. I have noticed that I was able to use the function in another class that extended "AppCompatActivity". I have also read from another thread that the function is not a part of the Adapter 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.

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). then pass your activity instance as created interface inside of adapter and use it inside of onclick listener. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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