How to get Firebase Data into a ListView?

前端 未结 2 1080
轻奢々
轻奢々 2021-01-25 14:28

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 th

相关标签:
2条回答
  • 2021-01-25 15:08

    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:

    public class MyObject {
    
       private String name;
       private String address;
       private String 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
            protected void populateView(View view, MyObject myObj, int position) {
               //Set the value for the views
               ((TextView)view.findViewById(R.id.xxx)).setText(myObj.getName());
               //...
            }
        };
        mListView.setAdapter(mAdapter);
    
    0 讨论(0)
  • 2021-01-25 15:12

    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..

    0 讨论(0)
提交回复
热议问题