- First of all add dependency
val lifecycle_version = "2.7.0"
implementation("androidx.lifecycle:lifecycle-livedata:$lifecycle_version")
implementation("androidx.lifecycle:lifecycle-viewmodel-ktx:$lifecycle_version")implementation ("com.squareup.retrofit2:retrofit:2.9.0")
implementation ("com.squareup.retrofit2:converter-gson:2.9.0") - after that create Retrofit file for setup Baseurl and connectivity.
object RetrofitClient {
private const val BASE_URL = "https://jsonplaceholder.typicode.com/"
val retrofit: Retrofit by lazy {
Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val apiInterface: ApiInterface by lazy {
retrofit.create(ApiInterface::class.java)
}
} - After that make interface class
interface ApiInterface {
@GET("posts")
suspend fun getPosts(): List<PostListResponseItem>
} - Create data model class according to your response
data class PostListResponseItem(
val body: String,
val id: Int,
val title: String,
val userId: Int
) - Create Adapter class for set recyclerview
class MainAdapter(private val list: ArrayList<PostListResponseItem>) :
RecyclerView.Adapter<MainAdapter.ViewHolder>() {
inner class ViewHolder(private val binding: ItemRowBinding) :
RecyclerView.ViewHolder(binding.root) {
fun bind(item: PostListResponseItem) {
binding.textView.text = item.title
binding.textView2.text = item.body
binding.textView3.text = item.userId.toString()
binding.textView4.text = item.id.toString()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.ViewHolder {
val laybinding = ItemRowBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ViewHolder(laybinding)
}
override fun onBindViewHolder(holder: MainAdapter.ViewHolder, position: Int) {
holder.bind(list[position])
}
override fun getItemCount(): Int {
return list.size
}
} - Create repository class
class MainRepository(private val apiInterface: ApiInterface) {
suspend fun getposts(): List<PostListResponseItem> {
return apiInterface.getPosts()
}
} - Create Viewmodel class
class MainViewmodel(private val mainRepository: MainRepository) : ViewModel() {
private var _postlist = MutableLiveData<List<PostListResponseItem>>()
val postList: LiveData<List<PostListResponseItem>>
get() = _postlist
fun getPostList() {
viewModelScope.launch(Dispatchers.IO) {
try {
val result = mainRepository.getposts()
_postlist.postValue(result)
} catch (e: Exception) {
}
}
}
} - Create viewmodel factory for viewmodel
class MainViewmodelFactory(private val repository: MainRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(MainViewmodel::class.java)) {
return MainViewmodel(repository) as T
}
throw IllegalArgumentException("Unknown Exception from Viewmodel")
}
} - Call viewmodel in MainActivity
val repository = MainRepository(RetrofitClient.apiInterface)
mainViewmodel =
ViewModelProvider(this, MainViewmodelFactory(repository))[MainViewmodel::class.java]
mainViewmodel.getPostList()
mainViewmodel.postList.observe(this, Observer {
binding.recyclerView.addItemDecoration(
DividerItemDecoration(
this,
DividerItemDecoration.VERTICAL
)
)
adapter = MainAdapter(it as ArrayList<PostListResponseItem>)
binding.recyclerView.adapter = adapter
}) - Note:- Make sure you have given the Internet Connection as well for call api from server
0 Comments