Skip to content Skip to sidebar Skip to footer

How To Get Firebase Data Into A Listview?

I am doing an Android App. I have been successful in writing to Firebase, I however am having a problem reading from Firebase into a ListView. If anyone can assist in getting the d

Solution 1:

You can use the Firebase UI library.

Just add the dependency:

dependencies {
    // FirebaseUI Database only
    compile 'com.firebaseui:firebase-ui-database:1.0.1'
}

Then define a simple class to map your object:

publicclassMyObject{

   privateString name;
   privateString address;
   privateString dateTime;

   public MyObject() {
   }
   ....

}

Then just use something like:

mRef = new Firebase("https://saica-sgb-77a4f.firebaseio.com/Meetings");
mListView = (ListView) findViewById(R.id.ListView);
mAdapter = new FirebaseListAdapter<MyObject>(this, MyObject.class, R.layout.myLayout, mRef) {
        @Override
        protectedvoidpopulateView(View view, MyObject myObj, int position) {
           //Set the value for the views
           ((TextView)view.findViewById(R.id.xxx)).setText(myObj.getName());
           //...
        }
    };
    mListView.setAdapter(mAdapter);

Solution 2:

Use ChildEventListener and put your this code: final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String(this,android.R.layout.simple_list_item_1, mMeetings); mListView.setAdapter(arrayAdapter);

inside the onChildAdded.... Also creat Model class to get data from firebase public class User {public String name;public String date;public String time;// Default constructor required for calls to // DataSnapshot.getValue(User.class) public User() {}public User(String name, String time, String date) {this.name = name; this.time= time;this.date=date;} thanks it will help you..

Post a Comment for "How To Get Firebase Data Into A Listview?"