0

This may be a bit long, but please help. I want to display records from firebase in listview and I am using childeventlistener for that. what I want is, when i Click on display button, It should display all the record in listview but it doesn't display anything. I have another button to add record into the database, When I add record and then try to display data from base, it display the correct data. Here's my code

 final Intent intent = getIntent();
    str1 = intent.getStringExtra("ClassName");

    p = intent.getIntExtra("ID", 0);
    String url = "https://onlookers-201096.firebaseio.com/" + str1;
    mRef = new Firebase(url);
    databaseReference = FirebaseDatabase.getInstance().getReferenceFromUrl(url);
    listView = findViewById(R.id.lv11);
    arrayAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_activated_1, id);
    listView.setAdapter(arrayAdapter);
    presentStudent = new LinkedHashSet<>();

     databaseReference.addChildEventListener(new com.google.firebase.database.ChildEventListener() {
          @Override
          public void onChildAdded(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {
              value = dataSnapshot.getValue(String.class);
              key = dataSnapshot.getKey();
              id.add(key + ":" + value);
              arrayAdapter.notifyDataSetChanged();
          }

          @Override
          public void onChildChanged(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {

          }

          @Override
          public void onChildRemoved(com.google.firebase.database.DataSnapshot dataSnapshot) {

          }

          @Override
          public void onChildMoved(com.google.firebase.database.DataSnapshot dataSnapshot, String s) {

          }

          @Override
          public void onCancelled(DatabaseError databaseError) {

          }
      });

What I can make out is it displays data only when a child is added. I want to display the data directly from the fire base as it is. Note: I tried using firebase listadapter but still was unable to get any solution Here is the question I asked earlier about FirebaseListadapter

Here is my database screenshot CSE is the child and it depends on user to choose this. If user enters CIVIL or MECHANICAL, it will create a new child with the respective name.

str1 = intent.getStringExtra("ClassName");
    String url = "https://onlookers-201096.firebaseio.com/" + str1;
    mRef = new Firebase(url);

I am using this link where str1 has the name of the child to be appended

  • regarding firebaselistadapter error, check this: https://stackoverflow.com/questions/47243957/firebase-list-adapter-constructor-error – Peter Haddad Apr 21 '18 at 17:04
  • Than you. But I have done all research before asking here. When I am trying `Firebaselistoptions` my application terminates. – aditya kumar singh Apr 21 '18 at 17:25
  • If you are interested, **[this](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is how you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Apr 23 '18 at 19:43

0 Answers0