簡體   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