Issue
I want to send an audio file from the adapter class to fragment that is retrieved from the firebase database in a fragment.
Here is my code for the Adapter class.
recyclerViewAdapter.java
public class recyclerViewAdapter extends FirebaseRecyclerAdapter<model, recyclerViewAdapter.recyclerViewHolder> {
public recyclerViewAdapter(@NonNull FirebaseRecyclerOptions<model> options) {
super(options);
}
@Override
protected void onBindViewHolder(@NonNull recyclerViewAdapter.recyclerViewHolder holder, int position, @NonNull model model) {
holder.recycler_view_title.setText(model.getTitle());
Glide.with(holder.recycler_view_image.getContext()).load(model.getImageUrl()).into(holder.recycler_view_image);
holder.recycler_view_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppCompatActivity appCompatActivity = (AppCompatActivity) v.getContext();
appCompatActivity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new DescriptionFragment(model.getTitle(), model.getDescription(), model.getImageUrl(), model.getAudioUrl())).addToBackStack(null).commit();
}
});
}
@NonNull
@Override
public recyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design, parent, false);
return new recyclerViewHolder(view);
}
public class recyclerViewHolder extends RecyclerView.ViewHolder {
private ImageView recycler_view_image;
private TextView recycler_view_title;
public recyclerViewHolder(@NonNull View itemView) {
super(itemView);
recycler_view_image = itemView.findViewById(R.id.recycler_view_image);
recycler_view_title = itemView.findViewById(R.id.recycler_view_title);
}
}
}
Solution
To send data between adapter
class to fragment
use interface
Create interface inside adapter class
OnDataSend dataSend;
public interface OnDataSend {
void onSend(String audioURL);
}
public void OnDataSend(OnDataSend dataSend){
this.dataSend = dataSend;
}
To send data put this in the adapter class
//To send data call
dataSend.onSend(/*Pass you data*/);
To receive data in the fragment
recyclerViewAdapter recyclerViewAdapter = new recyclerViewAdapter(/*...*/);
recyclerViewAdapter.OnDataSend(new recyclerViewAdapter.OnDataSend() {
@Override
public void onSend(String audioURL) {
//data received...
}
});
Detailed Answer
public class recyclerViewAdapter extends FirebaseRecyclerAdapter<model, recyclerViewAdapter.recyclerViewHolder> {
OnDataSend dataSend;
public recyclerViewAdapter(@NonNull FirebaseRecyclerOptions<model> options) {
super(options);
}
public void OnDataSend(OnDataSend dataSend) {
this.dataSend = dataSend;
}
@Override
protected void onBindViewHolder(@NonNull recyclerViewAdapter.recyclerViewHolder holder, int position, @NonNull model model) {
holder.recycler_view_title.setText(model.getTitle());
Glide.with(holder.recycler_view_image.getContext()).load(model.getImageUrl()).into(holder.recycler_view_image);
holder.recycler_view_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AppCompatActivity appCompatActivity = (AppCompatActivity) v.getContext();
appCompatActivity.getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new DescriptionFragment(model.getTitle(), model.getDescription(), model.getImageUrl(), model.getAudioUrl())).addToBackStack(null).commit();
//send data
dataSend.onSend(model.getAudioUrl());
}
});
}
@NonNull
@Override
public recyclerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_row_design, parent, false);
return new recyclerViewHolder(view);
}
public interface OnDataSend {
void onSend(String audioURL);
}
public class recyclerViewHolder extends RecyclerView.ViewHolder {
private ImageView recycler_view_image;
private TextView recycler_view_title;
public recyclerViewHolder(@NonNull View itemView) {
super(itemView);
recycler_view_image = itemView.findViewById(R.id.recycler_view_image);
recycler_view_title = itemView.findViewById(R.id.recycler_view_title);
}
}
}
Answered By - Marsad
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.