How to get Firebase Data into a ListView?

前端 未结 2 1079
轻奢々
轻奢々 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(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);
    

提交回复
热议问题