Reputation: 103
I am trying to pass a string variable from a fragment class to its adapter. The code written in the fragment class is :
public class RequestPendingFragment extends Fragment {
FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
DatabaseReference mRootRef = FirebaseDatabase.getInstance().getReference();
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
static ArrayList<Friend> req_friends;
static String team ;
public RequestPendingFragment() {
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
final String UID = firebaseUser.getUid();
Bundle bundle = this.getArguments();
team = bundle.getString("Team_name");
mRecyclerView = (RecyclerView) getView().findViewById(R.id.requestPendingFriend);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
mRootRef.child("users").child(UID).child("Teams").equalTo("team").addValueEventListener(new
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot snapshot = dataSnapshot ;
if(snapshot != null){
if(snapshot.hasChild("requests")){
DataSnapshot requests = dataSnapshot.child("requests");
ArrayList<Friend> requestFriend = new ArrayList<>();
for(DataSnapshot snapshot1: requests.getChildren()){
Friend friend = new Friend();
friend.setId(snapshot1.child("id").getValue(String.class));
friend.setFriendName(snapshot1.child("friendName").getValue(String.class));
friend.setStatus(snapshot1.child("status").getValue(String.class));
requestFriend.add(friend);
}
req_friends = requestFriend;
if(req_friends!=null && !req_friends.isEmpty()){
mAdapter = new RequestPendingAdapter(getActivity(), req_friends);
mRecyclerView.setAdapter(mAdapter);
}
}
And the relevant adapter code is :
public class RequestPendingAdapter extends
RecyclerView.Adapter<RequestPendingAdapter.RecyclerViewHolder>{
private ArrayList<Friend> mData;
private Context mContext;
public RequestPendingAdapter(Context mContext, ArrayList<Friend> mData) {
this.mData = mData;
this.mContext = mContext;
}
Now, Array List req_friends is being sent. Similarly, I need to pass string variable, 'team' from fragment class to adapter.I tried with the following changes in the Adapter code, but it is not working. Please guide how to do it correctly.
public class RequestPendingAdapter extends
RecyclerView.Adapter<RequestPendingAdapter.RecyclerViewHolder>{
private ArrayList<Friend> mData;
private Context mContext;
private String mteam ;
public RequestPendingAdapter(Context mContext, ArrayList<Friend> mData, String mteam) {
this.mData = mData;
this.mContext = mContext;
this.mteam= mteam;
}
Similarly in the fragment class I made this change :
if(req_friends!=null && !req_friends.isEmpty()){
mAdapter = new RequestPendingAdapter(getActivity(), req_friends);
mAdapter = new RequestPendingAdapter(getActivity(), team);
mRecyclerView.setAdapter(mAdapter);
The pop up error appearing is also attached.
Upvotes: 0
Views: 61
Reputation: 11
You need to make the below changes to your code.
if(req_friends!=null && !req_friends.isEmpty())
{
mAdapter = new RequestPendingAdapter(getActivity(),req_friends,team);
mRecyclerView.setAdapter(mAdapter);
}
Upvotes: 1