Home > OS >  Hello, I have updated my activity class as fragment, but I am not sure how to update the parts in th
Hello, I have updated my activity class as fragment, but I am not sure how to update the parts in th

Time:04-10

I have updated my activity class as fragment, but I am not sure how to update the parts in the photo.

photo

My Code :

package com.nisaefendioglu.recentearthquakes.fragment

import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.nisaefendioglu.recentearthquakes.R
import com.nisaefendioglu.recentearthquakes.RecyclerAdapter
import com.nisaefendioglu.recentearthquakes.model.EarthquakeModelItem
import com.nisaefendioglu.recentearthquakes.service.ApiClient
import kotlinx.android.synthetic.main.earthquake.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class Earthquake : Fragment((R.layout.earthquake)) {

    private var listUsers: MutableList<EarthquakeModelItem> = mutableListOf<EarthquakeModelItem>()
    private var adapter: RecyclerAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        listUsers = mutableListOf()

        recyclerview.layoutManager = LinearLayoutManager(this@Earthquake)

        adapter = RecyclerAdapter(
            this,
            listUsers
        )
        recyclerview.adapter = adapter
        getUsersData()

    }

    private fun getUsersData() {

        ApiClient.apiService.getEarthquakes().enqueue(object :
            Callback<MutableList<EarthquakeModelItem>> {
            override fun onFailure(call: Call<MutableList<EarthquakeModelItem>>, t: Throwable) {
                Log.e("error", t.localizedMessage)
            }

            override fun onResponse(
                call: Call<MutableList<EarthquakeModelItem>>,
                response: Response<MutableList<EarthquakeModelItem>>
            ) {
                val usersResponse = response.body()
                listUsers.clear()
                usersResponse?.let { listUsers.addAll(it) }
                adapter?.notifyDataSetChanged()
            }

        })

    }

}

Error : e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (26, 58): Type mismatch: inferred type is Earthquake but Context! was expected

e: /Users/nisa/AndroidStudioProjects/RecentEarthquakes/app/src/main/java/com/nisaefendioglu/recentearthquakes/fragment/Earthquake.kt: (29, 13): Type mismatch: inferred type is Earthquake but Context was expected

I have updated my activity class as fragment, but I am not sure how to update the parts in the photo. Can you help me?

I have updated my activity class as fragment, but I am not sure how to update the parts in the photo. Can you help me?

CodePudding user response:

  • Just pass context as activity or context you can't use this as context because you are inside a fragment

CodePudding user response:

Use requireContext() to get the context from fragment

  • Related