<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by nagendran p on Medium]]></title>
        <description><![CDATA[Stories by nagendran p on Medium]]></description>
        <link>https://medium.com/@nagu2013cse?source=rss-c70ea4ecc686------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*3JSuokRWeryjKRzuucAlJA.jpeg</url>
            <title>Stories by nagendran p on Medium</title>
            <link>https://medium.com/@nagu2013cse?source=rss-c70ea4ecc686------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 22:31:12 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@nagu2013cse/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Using Retrofit with Kotlin Coroutines in Android (MVVM)]]></title>
            <link>https://medium.com/@nagu2013cse/how-using-retrofit-with-kotlin-coroutines-in-android-mvvm-8cb225df5657?source=rss-c70ea4ecc686------2</link>
            <guid isPermaLink="false">https://medium.com/p/8cb225df5657</guid>
            <dc:creator><![CDATA[nagendran p]]></dc:creator>
            <pubDate>Mon, 20 Jul 2020 16:44:38 GMT</pubDate>
            <atom:updated>2020-07-20T16:44:38.200Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/810/1*IpNvb5XojVspBw3mKa7AvA.jpeg" /></figure><h3>What is Coroutines</h3><p>Essentially, coroutines are light-weight threads. It lets us write asynchronous, non-blocking code in a sequential way.</p><h3>What problems do coroutines solve?</h3><p>On Android, coroutines are a great solution to two problems:</p><ol><li><strong>Long running tasks</strong> are tasks that take too long to block the main thread.</li><li><strong>Main-safety </strong>allows you to ensure that any suspend function can be called from the main thread.</li></ol><h3>RxJava VS Coroutines</h3><ol><li>Coroutines is more efficient than RxJava as it uses less resources to execute the same task while also being faster in doing so. RxJava uses greater amounts of memory and requires more CPU time, which translates into higher battery consumption and possible UI interruptions for the user.</li><li>RxJava is a pretty heavy library with quite large variety of ready-to-use operators.</li><li>If the app needs to be simple and easily read by less experienced coders, use Coroutines</li><li>If the app needs high levels of data manipulation between fetching and emitting, use RxJava</li></ol><h3><strong>Role of Suspend and Resume</strong></h3><p>Coroutines build upon regular functions by adding two new operations. In addition to <strong>invoke</strong> (or call) and <strong>return</strong>, coroutines add <strong>suspend</strong> and <strong>resume</strong>.</p><ul><li><strong>suspend</strong> — pause the execution of the current coroutine, saving all local variables</li><li><strong>resume</strong> — continue a suspended coroutine from the place it was paused</li></ul><p>This functionality is added by Kotlin by the suspend keyword on the function. You can only call suspend functions from other suspend functions, or by using a coroutine builder like <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html">launch</a> to start a new coroutine.</p><p>Suspend and resume work together to replace callbacks.</p><h3>Main-safety with coroutines</h3><p>Using suspend doesn’t tell Kotlin to run a function on a background thread.</p><p>Using Dispatchers for which thread runs the coroutine</p><h4><strong><em>+ — — — — — — — — — — — — — — — — — -+<br>| Dispatchers.Main |<br>+ — — — — — — — — — — — — — — — — — -+<br>| Main thread on Android, interact |<br>| with the UI and perform light |<br>| work |<br>+ — — — — — — — — — — — — — — — — — -+<br>| — Calling suspend functions |<br>| — Call UI functions |<br>| — Updating LiveData |<br>+ — — — — — — — — — — — — — — — — — -+</em></strong></h4><h4><strong><em>+ — — — — — — — — — — — — — — — — — -+<br>| Dispatchers.IO |<br>+ — — — — — — — — — — — — — — — — — -+<br>| Optimized for disk and network IO |<br>| off the main thread |<br>+ — — — — — — — — — — — — — — — — — -+<br>| — Database* |<br>| — Reading/writing files |<br>| — Networking** |<br>+ — — — — — — — — — — — — — — — — — -+</em></strong></h4><h4><strong><em>+ — — — — — — — — — — — — — — — — — -+<br>| Dispatchers.Default |<br>+ — — — — — — — — — — — — — — — — — -+<br>| Optimized for CPU intensive work |<br>| off the main thread |<br>+ — — — — — — — — — — — — — — — — — -+<br>| — Sorting a list |<br>| — Parsing JSON |<br>| — DiffUtils |<br>+ — — — — — — — — — — — — — — — — — -+</em></strong></h4><h3>References for before get start the Project</h3><ul><li>Official Kotlin Coroutines docs<br>It’s good to read the official docs before using it.</li></ul><p><a href="https://kotlinlang.org/docs/reference/coroutines/basics.html">Basics - Kotlin Programming Language</a></p><ul><li>Official Kotlin Coroutines Codelabs</li></ul><p><a href="https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#0">Use Kotlin Coroutines in your Android App</a></p><ul><li>A great medium series introducing Kotlin Coroutines:</li></ul><p><a href="https://proandroiddev.com/async-operations-with-kotlin-coroutines-part-1-c51cc581ad33">Async Operations with Kotlin Coroutines — Part 1</a></p><ul><li>Team lead of Kotlin libraries talks about the blocking threads and suspending coroutines.</li></ul><p><a href="https://medium.com/@elizarov/blocking-threads-suspending-coroutines-d33e11bf4761">Blocking threads, suspending coroutines</a></p><p><strong>Add dependencies</strong></p><p>Add the following dependencies in your app level build.gradle.</p><pre>implementation &#39;com.google.android.material:material:1.1.0&#39;</pre><pre>implementation &#39;androidx.recyclerview:recyclerview:1.1.0&#39;<br>    implementation &#39;com.github.bumptech.glide:glide:4.9.0&#39;</pre><pre>//LifeCycle<br>    implementation &#39;androidx.lifecycle:lifecycle-common:2.2.0&#39;<br>    implementation &#39;androidx.lifecycle:lifecycle-runtime:2.2.0&#39;<br>    implementation &#39;android.arch.lifecycle:extensions:2.2.0&#39;<br>    implementation &#39;androidx.lifecycle:lifecycle-livedata-ktx:2.2.0&#39;</pre><pre>//Retrofit<br>    implementation &#39;com.squareup.retrofit2:retrofit:2.6.0&#39;<br>    implementation &#39;com.google.code.gson:gson:2.8.5&#39;<br>    implementation &#39;com.squareup.retrofit2:converter-gson:2.5.0&#39;<br>    implementation &#39;com.squareup.okhttp3:logging-interceptor:3.12.0&#39;</pre><pre>//Coroutines<br>    implementation &#39;org.jetbrains.kotlinx:kotlinx-coroutines-<br>android:1.3.5&#39;</pre><pre>android{<br>dataBinding<strong>{<br>    </strong>enabled=<strong>true</strong>;<br>   <strong>}<br>}</strong></pre><h3>Utils</h3><p>Firstly, the Enum status class:</p><pre><strong>package </strong>com.example.mycoroutinessample.util<br><br><strong>enum class </strong>Status {<br>    <strong>SUCCESS</strong>,<br>    <strong>ERROR</strong>,<br>    <strong>LOADING<br></strong>}</pre><p>Now, the Resource class:</p><pre><strong>package </strong>com.example.mycoroutinessample.util<br><br><strong>data class </strong>Resource&lt;<strong>out </strong>T&gt;(<strong>val status</strong>: Status,<strong>val data</strong>: T?,<strong>val message</strong>: String?) {<br><br>    <strong>companion object</strong>{<br>        <strong>fun</strong>&lt;T&gt; success(data:T):Resource&lt;T&gt; = Resource(status= Status.<strong>SUCCESS</strong>,data = data,message = <strong>null</strong>)</pre><pre>        <strong>fun</strong>&lt;T&gt; error(data:T?,message: String?):Resource&lt;T&gt; = Resource(status= Status.<strong>ERROR</strong>,data = data,message = message)</pre><pre>        <strong>fun</strong>&lt;T&gt; loading(data:T?):Resource&lt;T&gt; = Resource(status= Status.<strong>LOADING</strong>,data = data,message = <strong>null</strong>)<br>    }</pre><h3>Model</h3><pre>@Parcelize<br><strong>data class </strong>User(<br>                 @Expose<br>                 @SerializedName(<strong>&quot;id&quot;</strong>)<br>                 <strong>var id</strong>: String,<br>                 @Expose<br>                 @SerializedName(<strong>&quot;name&quot;</strong>)<br>                 <strong>var name</strong>: String,<br>                 @Expose<br>                 @SerializedName(<strong>&quot;title&quot;</strong>)<br>                 <strong>var title</strong>: String,<br>                 @Expose<br>                 @SerializedName(<strong>&quot;message&quot;</strong>)<br>                 <strong>var message</strong>: String<br>) : Parcelable</pre><h3>Network Layer</h3><p>Now, since we are using Retrofit for Network calls, let’s create a class that provides us the instance of the Retrofit Service class.</p><p><strong>Note</strong>: <em>We are going to come across some new keywords such as “suspend” in this Network layer. We are going to understand this later in this blog. First, let’s set up the project.</em></p><p><strong>Retrofit Service class</strong></p><pre><strong>import </strong>com.example.mycoroutinessample.data.model.User<br><strong>import </strong>com.google.gson.JsonObject<br><strong>import </strong>retrofit2.http.FieldMap<br><strong>import </strong>retrofit2.http.FormUrlEncoded<br><strong>import </strong>retrofit2.http.GET<br><strong>import </strong>retrofit2.http.POST<br><strong>import </strong>java.util.*<br><br><strong>interface </strong>ApiService {<br><br>    <br>    @FormUrlEncoded<br>    @POST(<strong>&quot;/posts/insert.php&quot;</strong>)<br>    <strong>suspend fun </strong>createUser(@FieldMap body: HashMap&lt;String, String&gt;): User<br><br>   @GET(<strong>&quot;/posts/show.php&quot;</strong>)<br>    <strong>suspend fun </strong>getUsers(): List&lt;User&gt;<br><em><br>    </em>@FormUrlEncoded<br>    @POST(<strong>&quot;/posts/update.php&quot;</strong>)<br>    <strong>suspend fun </strong>updateUser(@FieldMap body: HashMap&lt;String, String&gt;): User<br><br>    <em>// Delete note<br>    </em>@FormUrlEncoded<br>    @POST(<strong>&quot;/posts/delet.php&quot;</strong>)<br>    <strong>suspend fun </strong>deleteUser(@FieldMap body: HashMap&lt;String, String&gt;): JsonObject<br><br>}</pre><p><strong>Retrofit Builder class:</strong></p><pre><strong>import </strong>com.google.gson.GsonBuilder<br><strong>import </strong>okhttp3.OkHttpClient<br><strong>import </strong>okhttp3.logging.HttpLoggingInterceptor<br><strong>import </strong>retrofit2.Retrofit<br><strong>import </strong>retrofit2.converter.gson.GsonConverterFactory<br><br><strong>object </strong>ApiClient {</pre><pre>  //please use your own url</pre><pre>   <strong>var BASE_URL</strong>:String=<strong>&quot;http://localhost/posts/&quot; <br></strong><em><br>    </em><strong>fun </strong>apiClient(): Retrofit{<br><br>        <strong>val </strong>gson = GsonBuilder()<br>            .setLenient()<br>            .create()<br><br>        <strong>val </strong>interceptor = HttpLoggingInterceptor()<br>        interceptor.setLevel(HttpLoggingInterceptor.Level.<strong>BODY</strong>)<br>        <br>         <strong>val </strong>client = OkHttpClient.Builder().addInterceptor(interceptor).build()<br><br>        <strong>val </strong>retrofit = Retrofit.Builder()<br>            .baseUrl(<strong>BASE_URL</strong>)<br>            .client(client)<br>            .addConverterFactory(GsonConverterFactory.create(gson))<br>            .build()<br><br>        <strong>return </strong>retrofit<br><br>    }<br><br>}</pre><h3>ViewModel</h3><p><strong>Note</strong>: <em>We are going to come across some new keywords such =“liveData scope” in this ViewModel.</em></p><p>A LifecycleScope is defined for each Lifecycle object. LifecycleOwner could be an Activity or a Fragment. Any coroutine launched in this scope is canceled when the Lifecycle is destroyed. This helps us in avoiding memory leaks.</p><p>Here we have used <em>liveData</em>(Dispatchers.IO). If we observe the import statement:</p><pre><strong>import</strong> androidx.lifecycle.liveData</pre><p>Hence, the result of the function will be emitted as Live Data, which can be observed in the view (Activity or Fragment).</p><pre><strong>package </strong>com.example.mycoroutinessample.ui.main.viewmodel<br><br><strong>import </strong>androidx.lifecycle.ViewModel<br><strong>import </strong>androidx.lifecycle.liveData<br><strong>import </strong>com.example.mycoroutinessample.util.Resource<br><strong>import </strong>com.example.recyclerviewex.ApiService<br><strong>import </strong>kotlinx.coroutines.Dispatchers<br><strong>import </strong>java.lang.Exception<br><strong>import </strong>java.util.HashMap<br><br><strong>class </strong>MainViewModel(<strong>private val apiService</strong>: ApiService) : ViewModel() {<br><br>  <strong>fun </strong>getUsers()= <em>liveData</em>(Dispatchers.<strong>IO</strong>) <strong>{<br>         </strong>emit(Resource.loading(data = <strong>null</strong>))<br>        <strong>try </strong>{<br>            emit(Resource.success(data=<strong>apiService</strong>.getUsers()))<br>        }<strong>catch </strong>(exception: Exception){<br>            emit(Resource.error(data=<strong>null</strong>,message = exception.<strong>message</strong>?:<strong>&quot;Error occured&quot;</strong>))<br>        }<br>  <strong>}<br></strong>}</pre><p>We will be providing our View Model from a Factory class. So let’s construct our ViewModelFactory class:</p><pre><strong>package </strong>com.example.mycoroutinessample.ui.base<br><br><strong>import </strong>androidx.lifecycle.ViewModel<br><strong>import </strong>androidx.lifecycle.ViewModelProvider<br><strong>import<br></strong>com.example.mycoroutinessample.ui.main.viewmodel.MainViewModel<br><strong>import </strong>com.example.recyclerviewex.ApiService<br><strong>import </strong>java.lang.IllegalArgumentException<br><br><strong>class </strong>ViewModelFactory(<strong>private val apiService</strong>: ApiService) : ViewModelProvider.Factory{<br>    <strong>override fun </strong>&lt;T : ViewModel?&gt; create(modelClass: Class&lt;T&gt;): T {<br><br>        <strong>if</strong>(modelClass.isAssignableFrom(MainViewModel::<strong>class</strong>.<em>java</em>)){<br>            <strong>return </strong>MainViewModel(<strong>apiService</strong>)<strong>as </strong>T<br>        }<br><br>        <strong>throw </strong>IllegalArgumentException(<strong>&quot;Unknown class name&quot;</strong>)<br>    }<br>}</pre><h3>View</h3><p>MainActivity.class</p><pre><strong>package </strong>com.example.mycoroutinessample.ui.main.view<br><br><strong>import </strong>androidx.appcompat.app.AppCompatActivity<br><strong>import </strong>android.os.Bundle<br><strong>import </strong>android.view.View<br><strong>import </strong>android.widget.Toast<br><strong>import </strong>androidx.databinding.DataBindingUtil<br><strong>import </strong>androidx.lifecycle.Observer<br><strong>import </strong>androidx.lifecycle.ViewModelProviders<br><strong>import </strong>androidx.recyclerview.widget.DefaultItemAnimator<br><strong>import </strong>androidx.recyclerview.widget.LinearLayoutManager<br><strong>import </strong>com.example.mycoroutinessample.R<br><strong>import </strong>com.example.mycoroutinessample.data.model.User<br><strong>import </strong>com.example.mycoroutinessample.databinding.ActivityMainBinding<br><strong>import </strong>com.example.mycoroutinessample.ui.base.ViewModelFactory<br><strong>import </strong>com.example.mycoroutinessample.ui.main.adapter.UserListAdapter<br><strong>import </strong>com.example.mycoroutinessample.ui.main.viewmodel.MainViewModel<br><strong>import </strong>com.example.mycoroutinessample.util.Status<br><strong>import </strong>com.example.recyclerviewex.ApiClient<br><strong>import </strong>com.example.recyclerviewex.ApiService<br><strong>import </strong>com.google.gson.JsonObject<br><br><strong>class </strong>MainActivity : AppCompatActivity() {<br><br>    <strong>lateinit var mainBinding</strong>: ActivityMainBinding<br><br>    <strong>var userList </strong>= ArrayList&lt;User&gt;()<br><br><br>    <strong>private lateinit var viewModel</strong>: MainViewModel<br>    <strong>private lateinit var adapter</strong>: UserListAdapter<br><br>    <strong>override fun </strong>onCreate(savedInstanceState: Bundle?) {<br>        <strong>super</strong>.onCreate(savedInstanceState)<br>        setContentView(R.layout.<em>activity_main</em>)<br><br>        <strong>mainBinding </strong>= DataBindingUtil.setContentView(<strong>this</strong>, R.layout.<em>activity_main</em>)<br><br>        <strong>mainBinding</strong>.<em>mainActivity </em>= <strong>this<br><br>        </strong>setupViewModel()<br><br>        setupUI()<br><br>        setUpObserver()<br><br>    }<br><br><strong>private fun </strong>setupViewModel() {<br>      <strong>viewModel </strong>= ViewModelProviders.of(<br>            <strong>this</strong>,<br>            ViewModelFactory(ApiClient.apiClient().create(ApiService::<strong>class</strong>.<em>java</em>))<br>        ).get(MainViewModel::<strong>class</strong>.<em>java</em>)<br>   <br> }<br></pre><pre><strong>private fun </strong>setupUI() {<br>       <br> <strong>mainBinding</strong>.<strong>recyclerView</strong>.<em>layoutManager </em>=<br>       LinearLayoutManager(<strong>this</strong>, LinearLayoutManager.<em>VERTICAL</em>,<strong>false</strong>)<br><br>            <strong>mainBinding</strong>.<strong>recyclerView</strong>.setItemAnimator(DefaultItemAnimator())<br><br>       <strong>adapter </strong>= UserListAdapter(<strong>userList</strong>, <strong>object </strong>: UserListAdapter.ClicListener {<br><br>            <strong>override fun </strong>update(position: Int) {<br>            }<br><br>            <strong>override fun </strong>delete(position: Int) {<br>            }<br>        })</pre><pre><em>        </em><strong>mainBinding</strong>.<strong>recyclerView</strong>.<em>adapter </em>= <strong>adapter<br>   </strong>}<br><br><br>  <strong>private fun </strong>setUpObserver() {<br><br>    <strong>viewModel</strong>.getUsers().observe(<strong>this</strong>, <em>Observer </em><strong>{<br><br>      it</strong>?.<em>let </em><strong>{ </strong>resource <strong>-&gt;<br>         when </strong>(resource.<strong>status</strong>) {<br><br>             Status.<strong>SUCCESS </strong>-&gt; {<br>                  showProgress(<strong>false</strong>)<br>                  resource.<strong>data</strong>?.<em>let </em><strong>{ </strong>users <strong>-&gt;</strong>retrieveList(users) <strong>}<br>                    </strong>}</pre><pre>             Status.<strong>ERROR </strong>-&gt; {<br>                  showProgress(<strong>false</strong>)<br>                  Toast.makeText(<strong>this</strong>, <strong>it</strong>.<strong>message</strong>, Toast.<em>LENGTH_LONG</em>).show()<br>                    }</pre><pre>              Status.<strong>LOADING </strong>-&gt; {<br>                  showProgress(<strong>true</strong>)<br>                    }<br>                }<br><br>            <strong>}<br>        }</strong>)<br>    }<br><br>    <strong>private fun </strong>retrieveList(users: List&lt;User&gt;) {<br>        <strong>if </strong>(users.<strong>size </strong>&gt; 0) {<br>            <strong>userList</strong>.addAll(users)<br>            <strong>adapter</strong>.notifyDataSetChanged()<br>        }<br>    }<br><br><br>    <strong>private fun </strong>showProgress(status: Boolean) {<br>        <strong>if </strong>(status) {<br>            <strong>mainBinding</strong>.<strong>showProgress</strong>.<em>visibility </em>= View.<em>VISIBLE<br>        </em>} <strong>else </strong>{<br>            <strong>mainBinding</strong>.<strong>showProgress</strong>.<em>visibility </em>= View.<em>GONE<br>        </em>}<br>    }<br>}</pre><p>our <em>activity_main.xml</em></p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br><br></em>&lt;<strong>layout<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;<br>    xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;<br>    xmlns:tools=&quot;http://schemas.android.com/tools&quot;</strong>&gt;<br><br><br>    &lt;<strong>data</strong>&gt;<br>        &lt;<strong>import type=&quot;android.view.View&quot; </strong>/&gt;<br><br>        &lt;<strong>variable<br>            name=&quot;mainActivity&quot;<br>            type=&quot;com.example.mycoroutinessample.ui.main.view.MainActivity&quot; </strong>/&gt;<br><br>        &lt;<strong>variable<br>            name=&quot;userViewModel&quot;<br>            type=&quot;com.example.mycoroutinessample.ui.main.viewmodel.MainViewModel&quot; </strong>/&gt;<br>    &lt;/<strong>data</strong>&gt;<br>    &lt;<strong>FrameLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;match_parent&quot;</strong>&gt;<br>        &lt;<strong>LinearLayout<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;match_parent&quot;<br>            android:orientation=&quot;vertical&quot;<br>            tools:context=&quot;.view.MainActivity&quot;</strong>&gt;<em><br><br>            </em>&lt;<strong>androidx.recyclerview.widget.RecyclerView<br>                android:id=&quot;@+id/recyclerView&quot;<br>                android:layout_width=&quot;match_parent&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:text=&quot;Hello World!&quot;<br>                </strong>/&gt;<br><br>        &lt;/<strong>LinearLayout</strong>&gt;<br><br>        &lt;<strong>ProgressBar<br>            android:id=&quot;@+id/show_progress&quot;<br>            android:layout_width=&quot;match_parent&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_gravity=&quot;center&quot;<br>            </strong>/&gt;<br>       <br><br>    &lt;/<strong>FrameLayout</strong>&gt;<br><br>&lt;/<strong>layout</strong>&gt;</pre><p>UserListAdapter.java</p><pre><strong>package </strong>com.example.mycoroutinessample.ui.main.adapter<br><br><strong>import </strong>android.view.LayoutInflater<br><strong>import </strong>android.view.View<br><strong>import </strong>android.view.ViewGroup<br><strong>import </strong>android.widget.ImageView<br><strong>import </strong>android.widget.TextView<br><strong>import </strong>androidx.recyclerview.widget.RecyclerView<br><strong>import </strong>com.example.mycoroutinessample.R<br><strong>import </strong>com.example.mycoroutinessample.data.model.User<br><br><strong>class </strong>UserListAdapter(<strong>val userList</strong>: ArrayList&lt;User&gt;, <strong>val mListener</strong>: ClicListener) : RecyclerView.Adapter&lt;UserListAdapter.ViewHolder&gt;() {<br><br><br>    <strong>override fun </strong>onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {<br>        <strong>val </strong>v = LayoutInflater.from(parent.<em>context</em>).inflate(R.layout.<em>list_layout</em>, parent, <strong>false</strong>)<br>        <strong>return </strong>ViewHolder(v)<br>    }<br><br>   <strong>override fun </strong>getItemCount(): Int {<br>        <strong>return userList</strong>.<strong>size<br>    </strong>}<br><br>   <strong>override fun </strong>onBindViewHolder(holder: ViewHolder, position: Int){<br>        holder.bindItems(<strong>userList</strong>.get(position))<br>    }<br><br>    <strong>interface </strong>ClicListener {<br>        <strong>fun </strong>update(position: Int)<br>        <strong>fun </strong>delete(position: Int)<br><br>    }<br><br>    <strong>inner class </strong>ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView),View.OnClickListener {<br><br><br>        <strong>fun </strong>bindItems(user: User) {<br><br>            <strong>val </strong>name = <strong>itemView</strong>.findViewById&lt;TextView&gt;(R.id.<em>name</em>)<br>            <strong>val </strong>title = <strong>itemView</strong>.findViewById&lt;TextView&gt;(R.id.<em>title</em>)<br>            <strong>val </strong>message = <strong>itemView</strong>.findViewById&lt;TextView&gt;(R.id.<em>message</em>)<br><br>            <strong>val </strong>update = <strong>itemView</strong>.findViewById&lt;ImageView&gt;(R.id.<em>update</em>)<br>            <strong>val </strong>delete = <strong>itemView</strong>.findViewById&lt;ImageView&gt;(R.id.<em>delete</em>)<br><br>            name.<em>text </em>= user.<strong>name<br>            </strong>title.<em>text </em>= user.<strong>title<br>            </strong>message.<em>text </em>= user.<strong>message<br><br>            </strong>update.setOnClickListener(<strong>this</strong>)<br>            delete.setOnClickListener(<strong>this</strong>)<br>        }<br><br>        <strong>override fun </strong>onClick(view: View?) {<br><br>            <strong>if </strong>(view?.<em>id </em>== R.id.<em>update</em>)<br>                <strong>mListener</strong>.update(<em>adapterPosition</em>)<br>            <strong>if </strong>(view?.<em>id </em>== R.id.<em>delete</em>)<br>                <strong>mListener</strong>.delete(<em>adapterPosition</em>)<br><br>        }<br><br>    }<br>}</pre><p>list_layout.xml</p><pre><em>&lt;?</em><strong>xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;</strong><em>?&gt;<br></em>&lt;<strong>LinearLayout<br>    xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot; android:layout_width=&quot;match_parent&quot;<br>    android:layout_height=&quot;wrap_content&quot;<br>    android:orientation=&quot;vertical&quot;<br>    android:padding=&quot;5dp&quot;</strong>&gt;<br><br>    &lt;<strong>LinearLayout<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:orientation=&quot;horizontal&quot;</strong>&gt;<br>        &lt;<strong>TextView<br>            android:id=&quot;@+id/name&quot;<br>            android:layout_width=&quot;0dp&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_weight=&quot;1&quot;<br>            android:textSize=&quot;15sp&quot;<br>            android:layout_gravity=&quot;center&quot;<br>            android:gravity=&quot;left&quot;<br>            android:textStyle=&quot;bold&quot;</strong>/&gt;<br><br>        &lt;<strong>LinearLayout<br>            android:layout_width=&quot;0dp&quot;<br>            android:layout_height=&quot;wrap_content&quot;<br>            android:layout_weight=&quot;1&quot;</strong>&gt;<br><br>            &lt;<strong>ImageView<br>                android:id=&quot;@+id/update&quot;<br>                android:layout_width=&quot;0dp&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:src=&quot;@drawable/updateicon&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:layout_margin=&quot;5dp&quot;<br>                android:layout_gravity=&quot;center&quot;<br>                android:layout_weight=&quot;1&quot;</strong>/&gt;<br><br>            &lt;<strong>ImageView<br>                android:id=&quot;@+id/delete&quot;<br>                android:layout_width=&quot;0dp&quot;<br>                android:layout_height=&quot;wrap_content&quot;<br>                android:src=&quot;@drawable/deleteicon&quot;<br>                android:padding=&quot;5dp&quot;<br>                android:layout_margin=&quot;5dp&quot;<br>                android:layout_gravity=&quot;center&quot;<br>                android:layout_weight=&quot;1&quot;</strong>/&gt;<br><br><br>        &lt;/<strong>LinearLayout</strong>&gt;<br><br>    &lt;/<strong>LinearLayout</strong>&gt;<br><br><br>    &lt;<strong>TextView<br>        android:id=&quot;@+id/title&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:textSize=&quot;13sp&quot;<br>        android:gravity=&quot;left&quot;<br>        android:padding=&quot;5dp&quot;<br>        </strong>/&gt;<br>    &lt;<strong>TextView<br>        android:id=&quot;@+id/message&quot;<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;wrap_content&quot;<br>        android:textSize=&quot;13sp&quot;<br>        android:gravity=&quot;left&quot;<br>        android:padding=&quot;5dp&quot;<br>        </strong>/&gt;<br><br>    &lt;<strong>View<br>        android:layout_width=&quot;match_parent&quot;<br>        android:layout_height=&quot;1dp&quot;<br>        android:background=&quot;#000000&quot;<br>        </strong>/&gt;<br><br>&lt;/<strong>LinearLayout</strong>&gt;</pre><h3><strong>My Full Project Source Code here</strong></h3><p><a href="https://github.com/nagusmart/MyCoroutinesSample.git">nagusmart/MyCoroutinesSample</a></p><p>Happy Coding</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8cb225df5657" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>