filmov
tv
Android S7E7 : Firebase ListView - Save then Scroll to Last Added Item
![preview_player](https://i.ytimg.com/vi/CN59Gb8Dlzo/sddefault.jpg)
Показать описание
This an android firebase listview tutorial and example.We see how write/save, read/retrieve items to firebase database then smooth scroll to last added or saved item in the listview. We are using Firebase Realtime database for data persistence and custom listview for rendering this list of data.
This is tutorial that was requested and as you know these days we are covering almost all tutorials that are requested by community members.
Firebase Database is a popular realtime database and very powerful quite easy to use.
I take you guys through all the steps from setting up firebase, creating firebase app in the firebase console and finally to saving then retrieving data to and from firebase.
The data is written and read in realtime and the listview is always synced with the latest updates from firebase.
How to Save Data to Firebase Database
Saving data to Firebase database is very easy. This saving is sometimes called a `write` operation. Or `insert`.
We can easily save a custom object like say `User` or `Teacher` into firebase database. Then these will be stored in a JSONObject-like nodes.
In Firebase multiple users can save data at the same time by pushing the data via the `push()` method.
But first you need the Child node(like a Table) where we push that data. We can get it from our `DatabaseReference` object
We set the value we want to push or save to Firebase database via the `setValue()` method
How to Read/Retrieve Data From Firebase
There are two main ways of reading data from Firebase database:
1. Using ChildEventListener - Read and Return data node by node.
2. Uisng ValueEventListener - Read all data at once and return it.
The most commonly used way is the `ChildEventListener` method. However, our app in this case massively suits us to do it the second way, using the `ValueEventListener`. Why?
Well remember we want to write and read data to firebase then scroll over to the last added item. So we need all the data so that we can only scroll at the end, to the last added item.
So first we create a method called `retrieve()`. This method will retrieve data and bind to our ListView.
How to Smooth Scroll ListView to Recently Added Firebase Item
To scroll to the last added item in our ListView, we will make our ListView only scroll to the specified adapter position only when the user interface thread is ready.
Othwerwise we risk the scroll not happening as we expect since the user interface is going to be doing alot at this time.
We are interested in this case in the latter, enqueueing. The Handler does this just within the same thread.
So our scroll task will be enqueued and performed only when the UI thread is ready.
Hey, please watch the tutorial or read the article.
This is tutorial that was requested and as you know these days we are covering almost all tutorials that are requested by community members.
Firebase Database is a popular realtime database and very powerful quite easy to use.
I take you guys through all the steps from setting up firebase, creating firebase app in the firebase console and finally to saving then retrieving data to and from firebase.
The data is written and read in realtime and the listview is always synced with the latest updates from firebase.
How to Save Data to Firebase Database
Saving data to Firebase database is very easy. This saving is sometimes called a `write` operation. Or `insert`.
We can easily save a custom object like say `User` or `Teacher` into firebase database. Then these will be stored in a JSONObject-like nodes.
In Firebase multiple users can save data at the same time by pushing the data via the `push()` method.
But first you need the Child node(like a Table) where we push that data. We can get it from our `DatabaseReference` object
We set the value we want to push or save to Firebase database via the `setValue()` method
How to Read/Retrieve Data From Firebase
There are two main ways of reading data from Firebase database:
1. Using ChildEventListener - Read and Return data node by node.
2. Uisng ValueEventListener - Read all data at once and return it.
The most commonly used way is the `ChildEventListener` method. However, our app in this case massively suits us to do it the second way, using the `ValueEventListener`. Why?
Well remember we want to write and read data to firebase then scroll over to the last added item. So we need all the data so that we can only scroll at the end, to the last added item.
So first we create a method called `retrieve()`. This method will retrieve data and bind to our ListView.
How to Smooth Scroll ListView to Recently Added Firebase Item
To scroll to the last added item in our ListView, we will make our ListView only scroll to the specified adapter position only when the user interface thread is ready.
Othwerwise we risk the scroll not happening as we expect since the user interface is going to be doing alot at this time.
We are interested in this case in the latter, enqueueing. The Handler does this just within the same thread.
So our scroll task will be enqueued and performed only when the UI thread is ready.
Hey, please watch the tutorial or read the article.
Комментарии