2016-10-13 6 views
0

RecyclerViewデータをフラグメントに渡す方法と、フラグメントにどのコードを渡す必要がありますか。このコードはアクティビティで動作しますが、私は解決策を見たことがありますが、それで私のために働くことはできません。 、putExtras関数にあなたが持っているアクティビティに任意のバンドルを通過させるためのRecyclerViewの値をフラグメントに渡す

@Override 
     public void onClick(View view) { 
      Bundle bundle = new Bundle(); 
      int position = getAdapterPosition(); 
      Toast.makeText(context,"this is the position"+position, Toast.LENGTH_SHORT); 

      bundle.putSerializable("DATA", data.get(getAdapterPosition())); 
YourFragmentName f = new YourFragmentName(); 
f.setArguments(bundle); 
    getActivity().getSupportFragmentManager().beginTransaction() 
       .replace(R.id.your_container_frame_layout, 
       f).commit(); 
      } 

@Override 
     public void onClick(View view) { 
      //Open the new activity (THIS WORKS) 
      Intent intent = new Intent(context, NotifActivity.class); 
      Bundle bundle = new Bundle(); 
      int position = getAdapterPosition(); 
      Toast.makeText(context,"this is the position"+position, Toast.LENGTH_SHORT); 

      bundle.putSerializable("DATA", data.get(getAdapterPosition())); 


      intent.putExtras(bundle); 
      context.startActivity(intent); 
      } 

:このコードはrecyclerview

public MyHolder(View itemView) { 
     super(itemView); 
     textNotifTitle= (TextView) itemView.findViewById(R.id.textNotifTitle); 
     textNotifMessage = (TextView) itemView.findViewById(R.id.textNotifMessage); 
     TextDate = (TextView) itemView.findViewById(R.id.textDate); 
     itemView.setOnClickListener(this); 
     } 
     @Override 
     public void onClick(View view) { 
      //Open the new activity (THIS WORKS) 
      Intent intent = new Intent(context, NotifActivity.class); 
      Bundle bundle = new Bundle(); 
      int position = getAdapterPosition(); 
      Toast.makeText(context,"this is the position"+position, Toast.LENGTH_SHORT); 

      bundle.putSerializable("DATA", data.get(getAdapterPosition())); 


      intent.putExtras(bundle); 
      context.startActivity(intent); 
      } 

答えて

1

のアダプタからですこんにちはあなたは、コードを変更する必要がありますFragmentではsetArguments関数を使用する必要があります。

あなたは以下のコードによってフラグメントでバンドルを取得することができます:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     Bundle b = getArguments(); 
     if (b != null) { 
      //todo your code 
     } 

    } 
関連する問題