Reputation: 350
I want to send the String data in LibraryFragment to LibrarySongFragment.
LibraryFragment :
public class LibraryFragment extends Fragment {
private FirebaseRecyclerAdapter mAdapter;
@Nullable
@Override
public New onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
...
mAdapter = new LibraryAdapter(options, context);
recyclerView.setAdapter(mAdapter);
}
}
LibraryAdapter :
public class LibraryAdapter extends FirebaseRecyclerAdapter<MyModel, LibraryAdapter.MyViewHolder> {
...
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = getItem(getAdapterPosition()).getTitle();
((FragmentActivity) context).getSupportFragmentManager().beginTransaction()
.addToBackStack(null).replace(R.id.library_coordinator, new LibrarySongFragment()).commit();
}
});
}
}
}
I want to send the title that in MyViewHolder in LibraryAdapter. You can see the "String title = getItem(getAdpaterPosition()).getTitle();". Just send to LibrarySongFragment and post it in fragment_song_library.xml TextView.
Upvotes: 0
Views: 65
Reputation: 472
create inteface in fragment from where you are going to send the data
SendMessage SM;
public interface SendMessage{
void Senddata(String message,Boolean sent);
}
override this method to same fragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
SM = (SendMessage) getActivity();
} catch (ClassCastException e) {
throw new ClassCastException("Error in retrieving data. Please try again");
}
}
implement this fragment to the activity which hold these fragment
public class Activity extends AppCompatActivity implements sendfragment.SendMessage
override the message to the activity
@Override
public void Senddata(String message,Boolean sent) {
fragtwo f = (fragtwo) getSupportFragmentManager().findFragmentByTag(tag);
if(sent==true){
Toast.makeText(this, "data transfered", Toast.LENGTH_SHORT).show();
f.displaydata(message);
}
}
Create public method for sent data
public void displaydata(String massage){
String massageformat="received data is "+massage;
}
this is way to communicate between fragments in android
Upvotes: 1
Reputation: 195
You need to just create bundle of your data and set arguments in fragment object. i have modified your code. please refer
public class MyViewHolder extends RecyclerView.ViewHolder {
public MyViewHolder(View view) {
super(view);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = getItem(getAdapterPosition()).getTitle();
FragmentManager fragmentManager = ((Activity)context).getSupportFragmentManager();
Fragment fragment;
LibrarySongFragment librarySongFragment = new LibrarySongFragment();
Bundle bundle = new Bundle();
bundle.putString("KeyTitle",title);
librarySongFragment.setArguments(bundle);
fragmentManager.beginTransaction()
.addToBackStack(null).replace(R.id.library_coordinator, librarySongFragment).commit();
}
});
}
}
Upvotes: 1