Implement Interface
Start course
3h 12m

The course covers Recycler Views, which is one of the most important features of Android app development. They allow us to work with lists or grids of data with a ton of built-in optimizations. We'll then put your knowledge into practice by building an app to display a list of posts that we can scroll through, edit them, delete them, etc.

We'll also explore object-oriented programming with Kotlin, focusing on how to build objects, work with inheritance concepts and interfaces, and other highly advanced features. You'll also learn about classes and objects and how to work with them with the Kotlin programming language.

Intended Audience

This course is intended for beginners to Android app development or anyone who wants to master coding in Kotlin.


Since this is a beginner level course, there are no requirements, but any previous experience with coding would be beneficial.


Hello, and welcome back. In this video, we will continue where we left off in the last one and implement our interface in the main activity so we can handle the edit and delete methods from there. Right now, we have this interface which we added to the PostAdapter in the last video. So, in this video, we'll implement that interface here. So, first let's extend that here. So, next to AppCompactActivity, I'm going to put a comma and I want to implement that interface here. So, if I start typing out the name on PostClickListener, you see the one from my package pops up right here, com.example.alphablog. I'll hit 'Enter' and I'll get rid of this update message and as we do that you see mainActivity, my class declaration, is now red and if I hover over it, it's saying that I need to implement the members. 

So, right here I'm going to click on 'Implement members' and these are the two methods we had defined in our interface. So, I'm going to highlight both of them, click 'Okay' and there they are. Now, let's copy the logic that we had built for these two, edit and delete, in our adapter and bring it over here. So, back to PostAdapter, if I scroll up, here is my logic in the onClick method over here. So, I'm going to copy this from where I select the post to edit down to notifying that the item has changed. So, command C, go back to mainActivity, paste that for edit right here. Command V and I have a few red items here, that's fine. I'm going to go back to my PostAdapter and I'll copy this part for the delete. Command C, go back to main activity and paste it over here for delete. All right. 

And looks like I made some spelling mistakes over here, postion, that's interesting. So, this is going to be position, position, position, position. All right. And that was obviously because I misspelled it over here and it happened because of this right here. Okay, that's fine. Back to main activity. First, you see that dummyData is showing up as red, but we are initializing our dummyList over here. So, if I change this to dummyList, will that actually improve it? Let's see. If I do that, it still shows up as red so that's not good. Let me update that here dummyList, you see it's still red. So, why are these red

when I'm initializing my dummyList here? That's because we're initializing dummyList within our onCreate method and so the only reference we have to the list in this mainActivity classes right here val dummyList but we lose access to this variable or our reference as soon as the onCreate method ends, that's how variable scope works. So, when we try to access them here, dummyList and dummyList, we are not able to because we don't have this. So, to fix this, we can create our dummyList inside our class instead of inside our method, onCreate methods. All right. So, right here I'll say val dummyList = createMockData(). All right. So, now we're creating our list outside of onCreate and I can get rid of this from here. And you see the error associated with me referencing dummyList is gone, okay? Next, we have an error here for @PostAdapter. 

Now, within the adapter we could access this with the this@PostAdapter reference. If you look at PostAdapter, we were inside our postAdapter so we were able to use code like this. However, in mainActivity if you look at where we are setting the adapter, we are not storing this reference anywhere. We're just saying recyclerView.adapter and storing it to the postAdapter and creating it with the dummyList, but we're not storing this reference anywhere. So, let's create our adapter outside of this onCreate right here like we did for our dummyList and we'll set it to the recyclerView's adapter down here. So, first I'll say val adapter and I'll set it to PostAdapter and initialize it with my dummyList. So, this part right here, I'm doing it over here now that I have this adapter referencing this what I can do is inside my onCreate, I can simply set recyclerView.adapter to this adapter. And now that that's taken care of, I can access this adapter throughout my class. 

So, down here in my method instead of this@PostAdapter, I can simply say adapter here and adapter here. All right. And you see at any point if I highlight any of them, you see all the other references are highlighted, right? So, we know that we're working with the correct object. Okay, so now that these two methods are complete, I can call these methods instead of handling this functionality in my PostAdapter so let's take a look at our adapter. Over here, instead of handling this functionality here, I can simply call my methods. So, here where I'm handling my functionality for edit, I'll instead call the onEditPostClick method. So, right here, I'll get rid of all this and say onEditPost, let me make sure I get the name right onEditPostClick and onDeletePostClick and they both need the position. So, right here onEditPostClick and I'll pass in the position from here. 

So, I will, actually, correct this to position. All right, passing the position over here and for delete I will call onDeletePostClick and pass in the position and as you can see both of them are red and why is that? That's because I don't have a reference to my mainActivity anywhere here. See these methods I'm trying to call ain my mainActivity, right? I've implemented them here and I'm trying to call these but I'm in my PostAdapter. So, how do I reference my mainActivity while calling these methods? Well, let's take a look at how we can do that in a modular way. See, we are in our PostAdapter. So, let's take a look at our mainActivity and see where we are creating our PostAdapter object right here, right? We're calling PostAdapter with dummyList. So, this is where when we are creating this, so if we passed in the reference here somehow of our main activity, then this PostAdapter object will have that reference to this mainActivity. 

So, then when it's creating the PostAdapter in my constructor, I can reference that mainActivity. So, let's try it. We'll add in a parameter here in my constructor for PostAdapter and I'll call it val myActivity. And you see, as I'm typing this out, you see my mainActivity pops up here as the type. Basically, Android studio is anticipating what I'm trying to do, so, I'll select the type as my mainActivity right here, hit 'Enter' there we go. Now, I have a reference to my mainActivity that is getting passed in when my PostAdapter is getting created and since I have this, myActivity, I can go down here to my onClick and simply say myActivity.onEdit PostClick and myActivity.onDeletePostClick and as soon as I do that, you see all errors are gone from this adapter but when we look at our mainActivity, now you see there's this little red over here where we are creating the PostAdapter object. And that's because our adapter's constructor now expects me to pass in my mainActivity instance here, right? 

In PostAdapter, it's expecting this here in the constructor, so I have to pass in my mainActivity object, how do I do that? Well, we can easily do that using the this keyword, right? There you go. You see it says myActivity this and now all errors are gone from here and from my PostAdapter. So, let's run our code and make sure it works. All right. Here we go. Update. Updated title body Update, okay? Delete, Delete, still working perfectly. Great. All right, so this is looking great. However, we can make one more modification to our adapter. And bear with me here, we've been making updates when things have been working and I'm introducing new things one after the other but this is important for the modularity of our app. 

See, when we're creating this adapter, we're passing in my mainActivity object over here as reference, right? This, and this mainActivity implements the interface that we had created which was the onPostClickListener. So, basically, what I want you to remember is onPostClickListener, the interface is part of my mainActivity object and we know that by looking at this mainActivity right there. So, in PostAdapter, when we are accepting this, myActivity over here in the constructor, do we know that as well? Do we know that the mainActivity reference being passed in here implements that onPostClickListener object? Of course, we do because we know whoever wishes to use this adapter will need to implement the interface down here in order to use these two methods. And we know that the mainActivity does that already because we're calling those methods on my mainActivity right here. 

We're, basically, making that assumption here in our code based on how we've set up the interface and the fact that mainActivity extends or implements that interface that, that interface is part of my mainActivity object. So, instead of using the type of mainActivity here as a reference, we can instead use the interface as the reference. And then the code here in the adapter will be separate from needing the mainActivity type in its constructor because see what if I wanted this adapter to be used by the editActivity or some other activity, then I couldn't use this type of code because I'm using mainActivity but with the interface type instead it could be any activity calling this adapter and we can simply use the interface reference of that activity in here. 

So, we could do this instead of mainActivity, we can simply specify the type as onPostClickListener and you see the code still works, okay? Because now myActivity is actually this interface type of onPostClickListener and not myActivity type, but the name of the object seems a bit misleading. Since this is of type OnPostClickListener, I'll change this name over here to myListener, okay? And the two references here myListener, myListener, okay? And you can see that I don't get any errors. My code still works and this will grab the interface reference over here for whatever activity instance we pass in when we're constructing the adapter. So, let's run our app and make sure it still works. I'll run it. All right, let's pull it up. All right, scroll. Let's edit, update it, then another one, delete this one, delete this one, this one, this one. Great. Working exactly as we want. 

All right. So, we've implemented a full blog post app where we've used recyclerView and built the adapter and all the other components we needed step by step from scratch and believe it or not, this is the most difficult project in this course, especially, if you are a beginner to understand fully. But the good news is if you do grasp this and understand this, then building anything on top or implementing any other functionality will seem fairly easy in comparison and with that we've come to the conclusion of this section. We've covered a lot of ground here, starting with Object Oriented Programming and then moving on to building this app which implements most of the concepts that we learned in Object Oriented Programming in the form of the recyclerView implementation. 

So, I hope you enjoyed learning about it and we'll be able to use this in your own apps. Now, one thing about this data source that we used in here which we did in a mock database, this data was temporary, right? Every time we ran our app we were building a list of dummy posts and working on that running list, it was a temporary list. But in real life scenarios, you'll be working with real data from either local storage on your phone itself using a lightweight database like SQL light or Cloud Data Storage, like in Amazon Web Services or Google's Firebase. So, in the next section, we'll learn how to work with SQL light and how to save and manipulate data on your phone's local storage using it. All right, I hope you're excited. Congratulations on having made it this far and I'll see you in the next section.


About the Author

Mashrur is a full-time programming instructor specializing in programming fundamentals, web application development, machine learning and cyber security. He has been a technology professional for over a decade and has degrees in Computer Science and Economics. His niche is building comprehensive career focused technology courses for students entering new, complex, and challenging fields in today's technology space.

Covered Topics