How To Display Contents Retrieved From Firebase Realtime Database On A ListView As The Apps Loads
I'm trying to display records saved in Firebase Database to the user in a ListView as The Application loads but it's not working. The problem is that when the app loads for the first time the contents are not added to the ListView.
As I'm testing the app I noticed that the contents are not displayed as I open the app for the first time but only if I close the app by pressing the back button and then reopen it.
I add this method: adapter.notifyDataSetChanged(); to the onStart() as suggested by some guy but it didn't work.
This is the method that fetches the data:
private void fetchData() {
if (FirebaseDatabase.getInstance() != null) {
FirebaseDatabase.getInstance().goOffline();
}
FirebaseDatabase.getInstance().goOnline();
Query query = FirebaseDatabase.getInstance().getReference().child("User");
FirebaseRecyclerOptions<User> options =
new FirebaseRecyclerOptions.Builder<User>()
.setQuery(query, new SnapshotParser<User>() {
@NonNull
@Override
public User parseSnapshot(@NonNull DataSnapshot snapshot) {
s = snapshot.getChildrenCount();
Toast.makeText(getApplicationContext(), Long.toString(s), Toast.LENGTH_SHORT).show();
return new User(snapshot.child("fullName").getValue().toString(),
snapshot.child("userId").getValue().toString());
}
})
.build();
adapter = new FirebaseRecyclerAdapter<User, MyViewHolder>(options) {
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.database_contents, parent, false);
return new MyViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i,
@NonNull User user) {
myViewHolder.setTxtFullName(user.getFullName());
myViewHolder.setTxtUserId(user.getUserId());
myViewHolder.root.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), String.valueOf(i), Toast.LENGTH_SHORT).show();
}
});
}
};
}
and this is where I made the call:
@Override
protected void onStart() {
super.onStart();
fetchData();
recyclerView.setAdapter(adapter);
adapter.startListening();
adapter.notifyDataSetChanged();
}
I also tried calling the fetchData() method in onCreate.
I will like the contents to be displayed in the ListView as the App loads for the first time. Thanks in advance
Answer
here is simple way to get firebase data in listview as below..
final DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference().child("Contacts");
mDatabase.child(user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshott) {
list = new ArrayList<>();
for (DataSnapshot ds : dataSnapshott.getChildren()){
final String key_idd = ds.getKey();
Listdata listdata = new Listdata();
String name=ds.child("name").getValue(String.class);
listdata.setName(name);
listdata.setKey_id(key_idd);
list.add(listdata);
}
recycler = new RecyclerviewAdapter(MyContactActivity.this, list);
recyclerview.setAdapter(recycler);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.e("Error", "Failed to read user", error.toException());
}
});
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM