Assuming you have already set up an app in Android Studio, add a ListView
to a layout
(or skip if that's already done):
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Your toolbar, etc -->
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.CoordinatorLayout>
Now let's create a model for the data we're going to populate our ListView
with:
public class Person {
private String name
public Person() {
// Constructor required for Firebase Database
}
public String getName() {
return name;
}
}
Make sure your ListView
has an id, then create a reference to it in your Activity
and set its adapter to a new FirebaseListAdapter
:
public class MainActivity extends AppCompatActivity {
// ...
private ListView mListView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Find the ListView
mListView = (ListView) findViewById(R.id.list_view);
/*
* Create a DatabaseReference to the data; works with standard DatabaseReference methods
* like limitToLast() and etc.
*/
DatabaseReference peopleReference = FirebaseDatabase.getInstance().getReference()
.child("people");
// Now set the adapter with a given layout
mListView.setAdapter(new FirebaseListAdapter<Person>(this, Person.class,
android.R.layout.one_line_list_item, peopleReference) {
// Populate view as needed
@Override
protected void populateView(View view, Person person, int position) {
((TextView) view.findViewById(android.R.id.text1)).setText(person.getName());
}
});
}
}
After you've done that, add some data to your database and watch the ListView
populate itself.