Heads up! To view this whole video, sign in with your Courses Plus account or enroll in your free 7-day trial. Sign In Enroll
Preview
Start a free Courses trial
to watch this video
To learn RxJava we’re revisiting an app you’ve probably built before: a Todo list. We are including an implementation of a simple Todo app built without the use of RxJava.
To learn RX Java we're revisiting
an app you've probably built before.
0:00
A to do list.
0:04
I've included an implementation
of a simple to do app
0:06
built without the use of RX Java.
0:09
Let's take a look at the code and
see what's going on.
0:12
Here we can see that we have one activity,
0:15
the main activity We
have a data model object.
0:18
A to do is just as a description and
0:22
a boolean to indicate if this
to do item is completed.
0:25
We have a to do list.
0:28
So a to do list, is a list of to do's and
this to do listener.
0:32
Let's go to the to do listener,
0:37
that's a simple interface it has
a method called on to do is change.
0:39
So, if the to do list changes,
something is gonna listen for
0:43
changes to that and update that you are.
0:46
So our to do list keeps our items and
whenever we add or
0:50
remove an item from our to do list,
the listener will be called.
0:55
And also some other helpful methods to
set the listener up, to get the number of
1:00
items in our list, and to get
a specific to do from our list as well.
1:04
You'll notice that the to do list also
reads data in from Json and writes it
1:10
to Json in the to string, which is just
helpful for serializing this data.
1:16
If we need to we can also see that there
is something called a to do list filter so
1:23
this is a class.
1:27
And it implements the to do listener so
that means this receives a call back.
1:29
When the to do list changes in some way.
1:33
And here we can see at
some sort of filter.
1:37
It describes What to dos are being shown.
1:39
So, all.
1:42
That's all items, whether they
are completed or not complete.
1:43
Incomplete is a one,
and completed is a two.
1:47
And there's basically a mode,
a filter mode.
1:51
So, by default,
we want to show all the items.
1:53
And this filter class is responsible for.
1:56
Filtering the items out.
2:01
So if we're only looking
at the incomplete items.
2:02
Then we're going to take the to do list.
2:05
And we're going to compile just
the ones that are only incomplete.
2:07
And the same with complete.
2:11
We'll take the items.
2:12
And just compile a list of just the items
that are completed, and return those.
2:13
You'll notice that if it's all, we're
just going to return the list, itself.
2:18
So there's a to do list filter.
2:23
We can see that there is an adapter
to display these in our cycler view.
2:26
And this also listens for changes.
2:30
This it to do completed
change a listener here.
2:32
And they to do completed change listener.
2:37
Let's go ahead and look at what that is.
2:40
That is if an item is checked.
2:42
So if it changes from being not completed
to completed that item will then
2:46
be called on,
whatever implements this interface.
2:51
So we can see here that we have
a check box for each item.
2:54
And the first thing we're going
to do is actually set the on
2:59
check listener to normal.
3:02
That's because of this interesting
thing with Android where if you
3:03
call a set checked on a check box and
there's a listener set,
3:09
it's gonna get that call back with the new
value even if you're just setting it for
3:14
the first time with an initial.
3:19
Item value so in this case we don't want
to get the call back until the value
3:21
changes so we said to know we set the
value then we set up our listener here.
3:26
And we'll get changes to do the listener,
to do change listener.
3:32
Whenever an item is checked or unchecked.
3:37
We also have it on to do list change so
3:40
again this thing is also
implementing ToDoListener.
3:43
So it's listening for
changes to the list and
3:46
it's updating its data set whenever
items in our to do list change as well.
3:49
And then let's go back
to the main activity and
3:55
kinda see how this is all put together.
3:58
So here in our main activity,
we have all the views,
4:00
which is mainly the RecyclerView,
which has an adapter.
4:04
We also have the ability to
add a new item to the list
4:07
via an input which is an EditText and then
we have our list of data and our filter.
4:11
And we can see that we save our data and
we serialize it to the bundle.
4:16
That's for rotation and handling other
instances where our activities destroyed
4:22
and then recreated and
we can also see that.
4:26
We set up a to do list
filter with the list and
4:31
we set the filter to interact
as a part of the listener.
4:34
With the to do adapter so
there's all these listener's.
4:39
Calling to each other in each item and
it's a little bit complicated.
4:42
But we're going to see that our
next java helps us to simplify this
4:47
model quite a bit.
4:50
We can also see that we just look up
our recycler view we set our adapter.
4:53
And we also have our Ability
to add another todo item.
4:57
So in this case we have our ClickListener
for a button to add the todo.
5:03
When we click that button
we want to get the text.
5:07
Make sure it's not empty.
5:11
Add the item to the list.
5:13
Notify everything that
the changes have occurred.
5:14
Set the input box back to nothing.
5:18
Request focus away from our input box and
then dismiss the keyboard.
5:22
So this is basically going to dismiss
the keyboard and refresh our list.
5:25
And that's gonna happen by triggering
all of these listeners that are set up
5:29
between our adapter and
our filter and our to do list.
5:33
We can also see that we have this
spinner at the top in our toolbar, and
5:37
the spinner has three values.
5:41
It has the All, Completed,
and Incomplete values.
5:44
And when we select one of those we also
need to trigger all all those listeners to
5:47
change the value of what
UI is being displayed.
5:53
What items get put in our adapter
essentially, and we do that by
5:56
getting our filter and calling set filter
mode to the position which is an indicator
5:59
of which filter we're using and
we also have to notify our adapter.
6:04
Which needs to get the newest filtered
data from our filter as well.
6:10
So again these are all kind of
working together each of these is
6:15
tied to one another in all these
listeners and how they are set up.
6:19
Now there's probably
a better way to do this.
6:23
I know there is one at least
which is using our X Java.
6:25
There are other ways to do this but this
is a common thing you see in Android code
6:27
which is listeners, interfaces and
then working together to inform
6:32
when data has changed or when the UI
needs to be updated in some way.
6:37
So this is something you probably commonly
see in Android code, it may be something
6:42
you've written before and will see in
RX Java is that there's a much simpler
6:46
straight forward way to do it and
something that's also less error prone.
6:51
Okay so the first step that we want
to take when we're going to for
6:57
this project to use Rx-java is we need
to add some dependencies on Rx-java and
7:00
some other libraries that
will help us along the way.
7:06
So go ahead and go to your project and
open up your build.gradle for
7:08
the app module, and in here what we'll see
is at the very bottom, under dependencies,
7:13
we need to add some dependencies for
our RX Java, that's the first one.
7:19
We have here RX Java one dot one dot zero.
7:25
You can get the latest version
from the RX Java website.
7:28
But if you wanna use the code I'm using
here make sure you use that same version.
7:32
If you use a different version,
code might change.
7:38
And I'm also using rxandroid 1.1.0.
7:41
You'll notice that both of these
have the exact same version number.
7:43
And that's gonna be the case going forward
in the future, these should pretty much
7:48
have the same numbers because RX Android
will be updated along with RX Java.
7:51
What you also see is RX Binding and
what RX Binding does,
7:57
is it allows us to have observables and
subscriptions with common
8:01
UI elements, views in Android and
it's very helpful.
8:07
Comes in handy a lot of times.
8:11
So, arcs binding the default one is for
8:13
all the standard views that
come with Androids STK.
8:17
And we also have bindings for
8:19
the support library apt compact design or
cycle or view as well.
8:22
And then finally just because
it's easier let's go ahead and
8:27
use butter knife as well.
8:31
That'll make it easier to look
up our references to views and
8:32
you've probably used this before
in multiple workshops and courses.
8:36
So you should be familiar with that.
8:40
Once you've added these you're
gonna need to good ahead an sync so
8:42
we're good ahead sync now and
8:44
then our project should be ready to go to
start adding in RX Java functionality.
8:45
You need to sign up for Treehouse in order to download course files.
Sign up