Reputation: 141
I have a class Home
in which data is extracted from Firebase using a method getDatafromFirebase()
and when I Log the values in the loop it shows the value but when I add it into a list and update the adapter, the RecyclerView
shows null
for all the values.
public class Home extends Fragment {
public Home() {}
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
private DatabaseReference reference = FirebaseDatabase.getInstance ().getReference ();
ArrayList<FirebaseVideoDetials> list;
FirebaseVideoDetials details;
RecyclerViewAdapter adapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.fragment_home, container, false);
RelativeLayout without = view.findViewById(R.id.withoutInternet);
RelativeLayout mainLayout = view.findViewById(R.id.mainLayout);
recyclerView = view.findViewById(R.id.recyclerView);
layoutManager = new LinearLayoutManager(getActivity());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),LinearLayoutManager.HORIZONTAL));
details = new FirebaseVideoDetials();
list = new ArrayList<FirebaseVideoDetials>();
getDatafromFirebase();
adapter = new RecyclerViewAdapter(getActivity(), list);
recyclerView.setAdapter(adapter);
if(!Internetcheck.checkConnection(getActivity())){
without.setVisibility(View.VISIBLE);
mainLayout.setVisibility(View.INVISIBLE);
}else{
without.setVisibility(View.INVISIBLE);
mainLayout.setVisibility(View.VISIBLE);
}
return view;
}
public void getDatafromFirebase(){
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
DataSnapshot Videoreference = dataSnapshot.child("Videos");
for (DataSnapshot videoschild : Videoreference.getChildren()){
DataSnapshot description = videoschild.child("Description");
DataSnapshot duration = videoschild.child("Duration");
DataSnapshot title = videoschild.child("Title");
DataSnapshot thumbnail = videoschild.child("Thumbnail");
details.setDescription(String.valueOf(description.getValue()));
details.setDuration(String.valueOf(duration.getValue()));
details.setTitle(String.valueOf(title.getValue()));
details.setThumbnail(String.valueOf(thumbnail.getValue()));
Log.e("details",details.description);
list.add(details);
}
Log.e("Size",list.get(0).description);
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
}
Upvotes: 0
Views: 168
Reputation: 141
I solved it by calling the add function of Listview in a separate function:
public void getDatafromFirebase(){
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
list.clear();
DataSnapshot Videoreference = dataSnapshot.child("Videos");
for (DataSnapshot videoschild : Videoreference.getChildren()){
DataSnapshot description = videoschild.child("Description");
DataSnapshot duration = videoschild.child("Duration");
DataSnapshot title = videoschild.child("Title");
DataSnapshot thumbnail = videoschild.child("Thumbnail");
DataSnapshot date = videoschild.child("Date");
DataSnapshot time = videoschild.child("Time");
updatelist(description.getValue(String.class),
title.getValue(String.class),
String.valueOf(duration.getValue()),
thumbnail.getValue(String.class),
String.valueOf(date.getValue()),
String.valueOf(time.getValue()));
}
adapter.notifyDataSetChanged();
progressbar.setVisibility(View.GONE);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
public void updatelist(String desc,String title,String duration,String thumbnail,String Date,String time){
FirebaseVideoDetials details = new FirebaseVideoDetials();
details.setDescription(desc);
details.setDuration(duration);
details.setTitle(title);
details.setThumbnail(thumbnail);
details.setTime(time);
details.setDate(Date);
list.add(details);
}
Upvotes: 0
Reputation: 113
You must first initialize the adapter before calling adapter.notifyDataSetChanged
adapter = new RecyclerViewAdapter(getActivity(), list);
getDatafromFirebase();
Upvotes: 0
Reputation: 139019
To solve this, please move the following lines of code:
adapter = new RecyclerViewAdapter(getActivity(), list);
recyclerView.setAdapter(adapter);
In your onDataChange()
method right after:
Log.e("Size",list.get(0).description);
And comment the following line of code because is not needed:
//adapter.notifyDataSetChanged();
In order to make it work, you must initialize and set the adapter inside the callback, otherwise it will always be null
due the asynchronous behavior of this method. For this, I recommend you see my answer from this post . You can also take a look at this video for a better understanding.
Upvotes: 1