Last active
August 16, 2020 09:54
-
-
Save manoj-mili/5082cf26fc36ea538b48f8561504295c to your computer and use it in GitHub Desktop.
Initializing And Calling Our Adapter Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.carnot.recyclerviewlistadapter | |
import androidx.appcompat.app.AppCompatActivity | |
import android.os.Bundle | |
import kotlinx.android.synthetic.main.activity_main.* | |
class MainActivity : AppCompatActivity() { | |
lateinit var adapter: PostAdapter | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
//We initialise the postList just and abstract source | |
//which can be anything(Network, DB call, File etc) | |
val postList = getListOfPost(5) | |
adapter = PostAdapter() | |
rvTask.adapter = adapter | |
updatePostAdapter(postList) | |
fbRefresh.setOnClickListener { | |
// Just and example of data refresh which gets a new list and passes it to adapter. | |
val updatedPostList = getListOfPost(10) | |
updatePostAdapter(updatedPostList) | |
} | |
} | |
fun updatePostAdapter(postList: List<Post>) { | |
//important method which does the work of updating the items in Adapter | |
adapter.submitList(postList) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment