5

I am trying to retrieve data from firebase and display it on my listview using firebase-ui. The code runs fine but nothing is displayed on the list view. This is what I get from my logs:

W/ClassMapper: No setter/field for -KNRXdDOlA9nV6qxXvOl found on class com.example.sammie.coreteccontacts.Sacco

Here is my FirebaselistAdapter

DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference();
FirebaseListAdapter<Sacco> adapter = new FirebaseListAdapter<Sacco>(getActivity(), Sacco.class, android.R.layout.simple_list_item_1, mDatabaseReference) {
 @Override
protected void populateView(View view, Sacco sacco, int i) {
((TextView)view.findViewById(android.R.id.text1)).setText(Sacco.getName());
}
};
contactList.setAdapter(adapter);

Here is my Sacco class:

package com.example.sammie.coreteccontacts;

public class Sacco {

    String description;
    String location;
    static String name;

    public Sacco() {
    }

    public Sacco(String description, String location, String name) {
        this.name = name;
        this.description = description;
        this.location = location;
    }

    public static String getName() {
        return name;
    }

    public String getDescription() {
        return description;
    }

    public String getLocation() {
        return location;
    }
}

Here is a sample of the data from firebase

KNRWnG6BTkbGJQJVq9Qaddclose

description:"test description"

location: "test location"

name: "test name"

Community
  • 1
  • 1
Noel Omondi
  • 551
  • 1
  • 6
  • 23
  • 1
    you need to add setters for each variable inside `Sacco` class. e.g. `public void setDescription(String description){ this.description = description; }`. Also if `name` is unique to the object you probably don't want it to be `static` – wanpanman Jul 25 '16 at 16:27
  • I have added the setters but still get the same error – Noel Omondi Jul 26 '16 at 09:26

2 Answers2

6

There are no setter methods in your class.

public void setName(String name) {
    this.name = name;
}

public void setDescription(String description) {
    this.description = description;
}

public void setLocation(String location) {
    this.location = location;
}

BUT

-KNRWnG6BTkbGJQJVq9Qaddclose

This is a unique key which is to be obtained as an object containing your description, location and name.

W/ClassMapper: No setter/field for -KNRXdDOlA9nV6qxXvOl found on class com.example.sammie.coreteccontacts.Sacco

This shows that there is an error with your database listener's positioning. It is reading -KNRWnG6BTkbGJQJVq9Qaddclose as a variable to your Sacco class instead of an object of it.

Please check the JSON file and DatabaseReference. Most probably it should be like this:

  • UnderChilds <----- (ON CHILD LISTENER).
    • -KNRWnG6BTkbGJQJVq9Qaddclose
    • -KNRWnSYnuojkbGJQJVq9Qaddclos
Tanishq Sharma
  • 538
  • 2
  • 14
  • Let me try this, I'll get back to you – Noel Omondi Jul 26 '16 at 09:07
  • I realised I was calling the getName() method on the Sacco class so I changed it to call the getName() on the sacco object `((TextView)view.findViewById(android.R.id.text1)).setText(sacco.getName());` I added the setters methods to the Sacco class then changed the name variable to non static But I still get the same error how do I implement this `This shows that there is an error with your database listener's positioning. It is reading -KNRWnG6BTkbGJQJVq9Qaddclose as a variable to your Sacco class instead of an object of it.` – Noel Omondi Jul 26 '16 at 09:21
  • Where are you adding the Database Listener, on which node? Also, please show the JSON structure for example – Tanishq Sharma Jul 26 '16 at 11:48
  • [link](https://drive.google.com/file/d/0B9G2YgsHZohDc2l3dnFhcUxJd1U/view?usp=sharing) Here is an overview of my JSON structure, I think the problem is how the data is structured, i think I'll have to implement a child listener after getting the database reference, could you please advice on that. – Noel Omondi Jul 26 '16 at 12:26
  • Yes, I checked your data. You should put `ChildEventListener` on coretec-contacts and then you can use `Sacco ob = dataSnapshot.getvalue(Sacco.class);` in it. – Tanishq Sharma Jul 26 '16 at 12:45
  • Thank you that worked. Do you also know how I can get the current item title on the onItemClick listener for the firebase adapter? – Noel Omondi Jul 26 '16 at 13:48
  • Nice to know it worked. Do mark it as the solution for letting others know for community help. You can use `getItem()` for any item in adapter. – Tanishq Sharma Jul 26 '16 at 14:46
0

You have to get reference to your node:

DatabaseReference mDatabaseReference = FirebaseDatabase.getInstance().getReference().child("Your Node Name");
Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
A.M
  • 1
  • 1