I am facing an error "No adapter attached; skipping layout" - android

I am new in kotlin and retrofit, when I run this app it shows nothing in my device. But I check the logcat and found this errorNo adapter attached; skipping layout.Sometimes ago I post this question, some people answer this but this answer not correct
CountryActivity.kt
var recyclerView: RecyclerView = findViewById(R.id.countryRecyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
var apiInterface: CountryDataInterface =
CountryApiClient.getApiClient()!!.create(CountryDataInterface::class.java)
apiInterface.getCountryData().enqueue(object : Callback<List<Country>> {
override fun onFailure(call: Call<List<Country>>, t: Throwable) {}
override fun onResponse(call: Call<List<Country>>, response: Response<List<Country>>) {
val countryData = response.body()!!
recyclerView.adapter = CountryDataAdapter(countryData)
CountryDataAdapter.kt
class CountryDataAdapter(var countryDataList: List<Country>?):
RecyclerView.Adapter<CountryDataAdapter.RecyclerViewHolder>() {
class RecyclerViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
var countryName: TextView = itemView.findViewById(R.id.countryName)
var casesTotal: TextView = itemView.findViewById(R.id.casesTotal)
var casesToday: TextView = itemView.findViewById(R.id.casesToday)
var deathTotal: TextView = itemView.findViewById(R.id.deathTotal)
var deathToday: TextView = itemView.findViewById(R.id.deathToday)
var recoveredAll: TextView = itemView.findViewById(R.id.recoveredAll)
var activeAll: TextView = itemView.findViewById(R.id.activeAll)
var criticalAll: TextView = itemView.findViewById(R.id.criticalAll)
fun bindData(countryDataList: List<Country>?, position: Int){
countryName.text = countryDataList!!.get(position).countryName.toString()
casesTotal.text = countryDataList!!.get(position).cases.toString()
casesToday.text = countryDataList!!.get(position).todayCases.toString()
deathTotal.text = countryDataList!!.get(position).deathTotal.toString()
deathToday.text = countryDataList!!.get(position).deathToday.toString()
recoveredAll.text = countryDataList!!.get(position).recovered.toString()
activeAll.text = countryDataList!!.get(position).activePatient.toString()
criticalAll.text = countryDataList!!.get(position).critical.toString()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
var view: View = LayoutInflater.from(parent!!.context).inflate(R.layout.country_row,parent,false)
return RecyclerViewHolder(view)
}
override fun getItemCount(): Int {
return countryDataList!!.size
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
holder.bindData(countryDataList,position)
}
}

I think your issue is because you're calling recyclerView.adapter = CountryDataAdapter(countryData) in your Async function.
From what your post shows
override fun onResponse(call: Call<List<Country>>, response: Response<List<Country>>) {
val countryData = response.body()!!
recyclerView.adapter = CountryDataAdapter(countryData)
recyclerView.adapter = CountryDataAdapter(countryData) is in onResponse which is async, when your activity lunch, your adapter isn't set as it waits the result from the network to set it.
It would be better to set it to:
At the beginning of your activity add var countryData: List<Country> = ArrayList()
EDIT after checking Repo:
You get nothing because your call is getting into onFailure. You should add log to see this kind of things.
CountryActivity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_country)
val recyclerView: RecyclerView = findViewById(R.id.cRecyclerView)
var countryData: List<Country> = ArrayList()
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
recyclerView.adapter = CountryDataAdapter(countryData)
// recyclerView.addItemDecoration(DividerItemDecoration(this, DividerItemDecoration.VERTICAL))
val apiInterface: CountryDataInterface = CountryApiClient.getApiClient()!!.create(CountryDataInterface::class.java)
apiInterface.getCountryData().enqueue(object : Callback<List<Country>> {
override fun onFailure(call: Call<List<Country>>, t: Throwable) {
val data = Country(1, 2,3,4,5,6,7,8)
countryData = listOf(data)
recyclerView.adapter = CountryDataAdapter(countryData)
Log.d("onFailure", "ERROR")
}
override fun onResponse(call: Call<List<Country>>, response: Response<List<Country>>) {
if (response.isSuccessful) {
countryData = response.body()!!
recyclerView.adapter = CountryDataAdapter(countryData)
}
}
})
}
Adapter:
class CountryDataAdapter(private var countryDataList: List<Country>):
RecyclerView.Adapter<CountryDataAdapter.RecyclerViewHolder>() {
class RecyclerViewHolder(itemView: View):RecyclerView.ViewHolder(itemView) {
val countryName: TextView = itemView.findViewById(R.id.countryName)
val casesTotal: TextView = itemView.findViewById(R.id.casesTotal)
val casesToday: TextView = itemView.findViewById(R.id.casesToday)
val deathTotal: TextView = itemView.findViewById(R.id.deathTotal)
val deathToday: TextView = itemView.findViewById(R.id.deathToday)
val recoveredAll: TextView = itemView.findViewById(R.id.recoveredAll)
val activeAll: TextView = itemView.findViewById(R.id.activeAll)
val criticalAll: TextView = itemView.findViewById(R.id.criticalAll)
fun bindData(countryDataList: List<Country>, position: Int){
val item = countryDataList[position]
countryName.text = item.countryName.toString()
casesTotal.text = item.cases.toString()
casesToday.text = item.todayCases.toString()
deathTotal.text = item.deathTotal.toString()
deathToday.text = item.deathToday.toString()
recoveredAll.text = item.recovered.toString()
activeAll.text = item.activePatient.toString()
criticalAll.text = item.critical.toString()
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.country_row,parent,false)
return RecyclerViewHolder(view)
}
override fun getItemCount(): Int {
return countryDataList.size
}
override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {
holder.bindData(countryDataList,position)
}
}

Related

kotlin error recycler view with gridlayout type was mismatched

im facing errors in recyclerView here is //
data class categories(
val Name: String,
val Image:Int
)
here is model /////
object CategoriesModel {
fun getImages(): ArrayList<categories> {
val ImagesList = ArrayList<categories>()
val categoryFirst = categories(
"Memmals",R.drawable.memmals
)
ImagesList.add(categoryFirst)
val categorySecond = categories(
"Memmals",R.drawable.fish
)
ImagesList.add(categorySecond)
return ImagesList
}
}
//// adapter///
class CategoriesAdapter(private val itemList: ArrayList) :
RecyclerView.Adapter<CategoriesAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CategoriesAdapter.ViewHolder {
val view =LayoutInflater.from(parent.context)
.inflate(R.layout.memmals, parent,false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: CategoriesAdapter.ViewHolder, position: Int) {
holder.bind(categories("Memmals",2))
}
override fun getItemCount(): Int {
return itemList.size
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bind(category: categories) {
val textViewName = itemView.findViewById<TextView>(R.id.txtCategory)
textViewName.text = category.Name
val txtImage=itemView.findViewById<View>(R.id.image)
txtImage.id=category.Image
}
}
}
/////main activity///
class MainActivity : AppCompatActivity() {
private lateinit var mAdapter: CategoriesAdapter
private lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
binding= ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.recyclerView.setOnClickListener{
val intent=Intent(this,Level1::class.java)
startActivity(intent)
}
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
recyclerView.layoutManager = GridLayoutManager(this,2)
val itemList = ArrayList<CategoriesModel>()
// from here
itemList.add(categories("Memmals",R.drawable.memmals))
itemList.add(categories("Fishes",R.drawable.fish))
itemList.add(categories("Arthropods",R.drawable.dear))
itemList.add(categories("Yalk",R.drawable.yalk))
//to here errors are there
//Adapter setting
mAdapter = CategoriesAdapter(itemList)
recyclerView.adapter = mAdapter
}
}
/// here is the error///
MainActivity.kt: (34, 22): Type mismatch: inferred type is categories but CategoriesModel was expected
MainActivity.kt
val itemList = ArrayList<CategoriesModel>()
should be changed to
val itemList = ArrayList<categories>()
because you are using CategoriesModel object and expected is categories.

List is not updated after notifyDataSetChanged what should I do?

I am fetching JSON data from API and passing it in recycler view but if I want to fetch new data and display it in recycler view then I have to clear the list and then add new data in that list and notify the adapter that the data is changed but it is not updated what should I do?
MainActivity.kt
class MainActivity : AppCompatActivity() {
private lateinit var recipeViewModel: RecipeViewModel
private lateinit var mainBinding: ActivityMainBinding
private lateinit var recipeAdapter: RecipeAdapter
private lateinit var recipeItemList: ArrayList<Hit>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
recipeViewModel =
ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory
.getInstance(application)
)[RecipeViewModel::class.java]
recipeItemList = arrayListOf()
mainBinding.recyclerView.layoutManager = LinearLayoutManager(this)
mainBinding.recyclerView.hasFixedSize()
recipeAdapter = RecipeAdapter(this)
mainBinding.recyclerView.adapter = recipeAdapter
recipeViewModel.recipeLiveData.observe(this, Observer { recipeItems ->
recipeItemList.addAll(recipeItems.hits)
recipeAdapter.updateRecipes(recipeItemList)
Log.d("RESPONSE", recipeItems.toString())
Log.d("List size", recipeAdapter.itemCount.toString())
})
searchRecipeName()
}
private fun searchRecipeName() {
mainBinding.searchRecipeFabBtn.setOnClickListener {
val view = layoutInflater.inflate(R.layout.recipe_search_layout, null)
val searchRecipeET = view.findViewById<EditText>(R.id.searchRecipeET)
val searchRecipeBtn = view.findViewById<Button>(R.id.searchRecipeBtn)
val bottomSheetDialog = BottomSheetDialog(this)
bottomSheetDialog.apply {
this.setContentView(view)
this.show()
}
searchRecipeBtn.setOnClickListener {
val recipeName = searchRecipeET.text.toString()
searchRecipeName(recipeName, searchRecipeET, bottomSheetDialog)
}
}
}
private fun searchRecipeName(
recipeName: String,
searchRecipeET: EditText,
bottomSheetDialog: BottomSheetDialog
) {
if (recipeName.isEmpty()) {
searchRecipeET.error = "Please enter recipe name"
} else {
recipeViewModel.getRecipes(recipeName)
bottomSheetDialog.dismiss()
}
}
}
RecipeAdapter.kt
class RecipeAdapter(val context: Context) : RecyclerView.Adapter<RecipeAdapter.RecipeViewHolder>() {
private val recipesList: ArrayList<Hit> = arrayListOf()
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.recipe_items_layout, null, false)
return RecipeViewHolder(view)
}
override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
val currentItem = recipesList[position]
holder.recipeImageView.load(currentItem.recipe.image)
holder.recipeNameText.text = currentItem.recipe.label
}
override fun getItemCount(): Int {
return recipesList.size
}
class RecipeViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {
val recipeImageView: ImageView = itemView.findViewById(R.id.recipeImageView)
val recipeNameText: TextView = itemView.findViewById(R.id.recipeNameText)
}
fun updateRecipes(newRecipesList: ArrayList<Hit>){
recipesList.clear()
Log.d("RECIPE SIZE", "${recipesList.size}")
recipesList.addAll(newRecipesList)
notifyDataSetChanged()
}
}
This may be helpful.
Be careful of this :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mainBinding = ActivityMainBinding.inflate(layoutInflater)
setContentView(mainBinding.root)
recipeViewModel =
ViewModelProvider(
this,
ViewModelProvider.AndroidViewModelFactory
.getInstance(application)
)[RecipeViewModel::class.java]
recipeItemList = arrayListOf()
mainBinding.recyclerView.layoutManager = LinearLayoutManager(this)
mainBinding.recyclerView.hasFixedSize()
recipeAdapter = RecipeAdapter(this)
mainBinding.recyclerView.adapter = recipeAdapter
recipeViewModel.recipeLiveData.observe(this, Observer { recipeItems ->
// You're adding items here but never clear the list
// list will be bigger every time you'll be notified
// recipeItemList.addAll(recipeItems.hits
// recipeAdapter.updateRecipes(recipeItemList)
// Do this instead
recipeItemList = recipeItems.hits
recipeAdapter.updateRecipes(recipeItemList)
Log.d("RESPONSE", recipeItems.toString())
Log.d("List size", recipeAdapter.itemCount.toString())
})
searchRecipeName()
}
Also, here: It's a little better to do this (https://stackoverflow.com/a/10298038/4221943)
fun updateRecipes(newRecipesList: ArrayList<Hit>){
recipesList = newRecipesList
Log.d("RECIPE SIZE", "${recipesList.size}")
notifyDataSetChanged()
}
BTW it will always be more efficient to use the more specific change events if you can. Rely on notifyDataSetChanged() as a last resort. It is also good practice to use notifyItemInserted(mItems.size() - 1) for "easier" solution.
You could convert the RecyclerView.Adapter into a ListAdapter:
class RecipeAdapter(val context: Context) : ListAdapter<Hit, RecipeAdapter.RecipeViewHolder>(RecipeDiffCallback()) {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecipeViewHolder {
val layoutInflater = LayoutInflater.from(context)
val view = layoutInflater.inflate(R.layout.recipe_items_layout, null, false)
return RecipeViewHolder(view)
}
override fun onBindViewHolder(holder: RecipeViewHolder, position: Int) {
val currentItem = getItem(position)
holder.recipeImageView.load(currentItem.recipe.image)
holder.recipeNameText.text = currentItem.recipe.label
}
class RecipeViewHolder(itemView: View) :RecyclerView.ViewHolder(itemView) {
val recipeImageView: ImageView = itemView.findViewById(R.id.recipeImageView)
val recipeNameText: TextView = itemView.findViewById(R.id.recipeNameText)
}
}
class RecipeDiffCallback : DiffUtil.ItemCallback<Hit>() {
// Change this condition based on the attribute of `Hit` that will change
override fun areItemsTheSame(oldItem: Hit, newItem: Hit): Boolean = oldItem.id == newItem.id
override fun areContentsTheSame(oldItem: Hit, newItem: Hit): Boolean = oldItem == newItem
}
Then update its content with the submitList method.
Every item not satisfying the RecipeDiffCallback conditions will be automatically updated:
recipeViewModel.recipeLiveData.observe(this, Observer { recipeItems ->
recipeAdapter.submitList(recipeItems.hits)
})

RecyclerView Adapter with ViewDataBinding

I am creating a GenericAdapter for handling single row|item layouts. Everything working fine only view-binding is not updating data..
I want to get RecyclerView.ViewHolder binding in callback ,I know I can bind it in adapter using BR.item and executePending
I want viewDataBinding context in a Callback
holder.binding.name.text = mutableList[pos]
Above line in TestActivity not working properly
GenericAdapter.kt
class GenericAdapter<T,VB:ViewDataBinding>(
var items:MutableList<T>,
#LayoutRes val resLayoutID:Int,
val onBind:(holder:GenericViewHolder<T,VB>,pos:Int) -> Unit
): RecyclerView.Adapter<GenericViewHolder<T,VB>>() {
lateinit var mItemBinding:VB
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): GenericViewHolder<T,VB> {
val layoutInflater = LayoutInflater.from(parent.context)
mItemBinding = DataBindingUtil.inflate(layoutInflater, resLayoutID, parent, false)
return GenericViewHolder(mItemBinding)
}
override fun onBindViewHolder(holder: GenericViewHolder<T,VB>, position: Int) {
onBind(holder,position)
}
override fun getItemCount(): Int = items.size
}
GenericViewHolder.kt
class GenericViewHolder<T,VB: ViewDataBinding>(val binding: VB)
:RecyclerView.ViewHolder(binding.root){
val mItemBinding:VB = binding
}
TestActivity.kt
class TestActivity:AppCompatActivity() {
lateinit var recyclerView: RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_test)
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
populateList()
}
private fun populateList(){
val mutableList = mutableListOf<String>("apple","mango","tutti fruti","apricot",
"apple","mango","tutti fruti","apricot",
"apple","mango","tutti fruti","apricot",
"apple","mango","tutti fruti","apricot")
val mAdapter = GenericAdapter<String,ItemCountryBinding>(mutableList,R.layout.item_country){ holder,pos ->
//val nameTv = holder.itemView.findViewById<TextView>(R.id.name)
//nameTv.text = mutableList[pos]
holder.binding.name.text = mutableList[pos]
}
recyclerView.adapter = mAdapter
}
}
Where as below code working fine
val nameTv = holder.itemView.findViewById<TextView>(R.id.name)
nameTv.text = mutableList[pos]
I would recommend do onBind inside view holder.
I don't see reason why you need this callback.
You already passed mutableList to the adapter.
For example here is my ViewHolder
class NewsViewHolder(private val binding: ListItemNewsBinding) : RecyclerView.ViewHolder(binding.root) {
fun bind(news: RedditPost, itemClick: (RedditPost, View) -> Unit) {
with(binding) {
root.transitionName = news.thumbnail
root.setOnClickListener { itemClick.invoke(news, binding.root) }
title.text = news.title
image.setImageUrl(news.thumbnail)
author.text = news.author
xHoursAgo.text = news.created_utc
numComments.text = news.numComments
}
}
}
And
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
if (isLoadingMore && position == (itemCount - 1)) return
(holder as? NewsViewHolder)?.bind(news[position], itemClick)
}

How to sort encrypted ArrayList and show this in a RecyclerView

My target is to sort in alphabetical order data from an ArrayList with encrypted data and put correct order into a RecyclerView.
I'm able to show decrypted data into RecyclerView, but in the same order witch I use to add item into the ArrayList (see code in adapter).
I tried to sort with sortby{}without success (see code in activity). The result is an partial order and onItemClick not return correct position.
My activity that extend adapter:
class HomeActivity : AppCompatActivity(),DocumentAdapter.OnItemClickListener {
companion object{
lateinit var dbHandler: DBHandler
}
private var documentslist = ArrayList<PassItem>()
private lateinit var adapter : RecyclerView.Adapter<*>
private lateinit var rv : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
dbHandler = DBHandler(this, null, null,1)
viewDocuments()
val fab = findViewById<FloatingActionButton>(R.id.fabAdd)
fab.setOnClickListener {
val i = Intent(this, AddDocument :: class.java)
startActivity(i)
}
}
#SuppressLint("WrongConstant")
private fun viewDocuments(){
documentslist = dbHandler.getDocuments(this)
//documentslist.sortBy { it.PassItemApp.toString() } ---> this sort encrypted data then not in alphabetic order and onItemClick return uncorrect position
adapter = DocumentAdapter(this,documentslist,this)
rv = findViewById(R.id.recyclerView)
rv.layoutManager = LinearLayoutManager(this, LinearLayout.VERTICAL,false)
rv.adapter = adapter
}
override fun onItemClick(position: Int) {
val intent = Intent(this#HomeActivity,DocActivity::class.java)
intent.putExtra("clickedPosition", position)
startActivity(intent)
}
override fun onResume() {
viewDocuments()
super.onResume()
}
and my adapter:
class DocumentAdapter(val mCtx: Context, private val documents: ArrayList<PassItem>, val listener: OnItemClickListener) : RecyclerView.Adapter<DocumentAdapter.ViewHolder>(){
interface OnItemClickListener{
fun onItemClick(position : Int)
}
inner class ViewHolder(itemView : View) : RecyclerView.ViewHolder(itemView), View.OnClickListener{
val txtDocApp : TextView = itemView.findViewById(R.id.txtDocApp)
init {
itemView.setOnClickListener(this)
}
override fun onClick(v: View?) {
val position : Int = adapterPosition
if (position != RecyclerView.NO_POSITION){
listener.onItemClick(position)
}
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): DocumentAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.item_pass_layout,parent,false)
return ViewHolder(v)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val document : PassItem = documents[position]
//Decryption of item showed in Recyclerview
val initialisationVector = document.PassItemIvApp
val bytesIV = Base64.decode(initialisationVector, Base64.NO_WRAP)
val oisIV = ObjectInputStream(ByteArrayInputStream(bytesIV))
val initializationVectorIV = oisIV.readObject() as ByteArray
val encryptedText = document.PassItemApp
val bytesText = Base64.decode(encryptedText, Base64.NO_WRAP)
val oisText = ObjectInputStream(ByteArrayInputStream(bytesText))
val textByteArray = oisText.readObject() as ByteArray
val key = document.PassItemKeyApp
val keyText = Base64.decode(key, Base64.NO_WRAP)
val oisKey = ObjectInputStream(ByteArrayInputStream(keyText))
val keyToDecrypt = oisKey.readObject() as SecretKey
val aesDecrypt = Decrypted(keyToDecrypt,initializationVectorIV)
val decryptedByteArray = aesDecrypt.decrypt(textByteArray)
val textAfterDecryption = decryptedByteArray.toString(charset("UTF_8"))
//Data in Recyclerview
holder.txtDocApp.text = textAfterDecryption
}
override fun getItemCount(): Int {
return documents.size
}
}

using Retrofit enqueue calls getting problem

getting this error for enqueue for kotlin android and it is showing Unresolved reference: enqueue
my code
class NewsActivity : AppCompatActivity() {
private lateinit var mRecyclerView: RecyclerView
private lateinit var mProgressBar: ProgressBar
private lateinit var mAdapter: NewsAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_news)
mRecyclerView = findViewById(R.id.recycler_view)
mProgressBar = findViewById(R.id.progress_circular)
getItemList()
}
private fun getItemList() {
mProgressBar.visibility = View.VISIBLE
val apical = ApiClient.create().getData()
apical.enqueue(object : retrofit2.Callback<List<DataModelItem>> {
override fun onFailure(call: Call<List<DataModelItem>>, t: Throwable) {
mProgressBar.visibility = View.GONE
}
override fun onResponse(
call: Call<List<DataModelItem>>,
response: Response<List<DataModelItem>>) {
if (response.isSuccessful){
val itemListNews = response.body()!!
mAdapter = NewsAdapter(itemListNews)
val layoutManager = LinearLayoutManager(this#NewsActivity)
mRecyclerView.layoutManager = layoutManager
mRecyclerView.adapter = mAdapter
}
}
})
}
}
this is News activity
class NewsAdapter(private val itemList : List<DataModelItem>) : RecyclerView.Adapter<NewsAdapter.CustomViewHolder>() {
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): NewsAdapter.CustomViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.item_view, parent, false)
return CustomViewHolder(view)
}
override fun getItemCount(): Int {
return itemList.size
}
override fun onBindViewHolder(holder: NewsAdapter.CustomViewHolder, position: Int) {
val itemData = itemList.get(position)
Picasso.get().load(itemData.images).into(holder.img1)
holder.Titlenews.setText(itemData.title)
holder.txt_description.setText(itemData.summary)
holder.publish_time.setText(itemData.publish_date)
}
inner class CustomViewHolder(view: View): RecyclerView.ViewHolder(view){
var img1:ImageView
var Titlenews: TextView
var txt_description: TextView
var publish_time: TextView
init {
img1 = view.findViewById(R.id.item_image)
Titlenews = view.findViewById(R.id.Titlenews)
txt_description = view.findViewById(R.id.txt_description)
publish_time = view.findViewById(R.id.publish_time)
}
}
}
this is the news adapter
object ApiClient {
var BASE_URL = ""
fun create() :Api{
val retrofit = Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create())
.baseUrl(BASE_URL)
.build()
return retrofit.create(Api::class.java)
}
}
this is Api client
Help required
I want not getting a solution as always it is giving me an error for enqueue .
Try changing
apical.enqueue(object : retrofit2.Callback<List<DataModelItem>> {
to
apical.enqueue(object : retrofit.Callback<List<DataModelItem>> {

Categories

Resources