适配器类的访问意图



我正在尝试获取一个适配器类,以获取存储在另一个适配器类别中的意图。其想法是,第一个适配器类将存储一个意图,并启动第二个适配器类(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!它是空的吗?还有一些小指南:在recycleriew.adapter中创建一个接口,在父活动(适配器所在的位置(中实现该接口,然后将活动实例作为适配器中创建的接口传递,并在onclick监听器中使用。这个想法是将意图的逻辑传递给活动。接口:

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

最新更新