I try to load image with android studio kotlin app using Picasso. i am not able to load image.
i have written my code below. could any one tell me what mistakes i made in my code.
in Mainactivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myApi = Retrofit.Builder()
.baseUrl("http://192.168.59.85")
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(MyData::class.java)
myApi.getData().enqueue(object :Callback<List<MydatsItem>>{
override fun onResponse(
call: Call<List<MydatsItem>>,
response: Response<List<MydatsItem>>
) {
val recycle = findViewById<RecyclerView>(R.id.recyclr)
recycle.layoutManager = LinearLayoutManager(this@MainActivity)
response.body()?.let {
val adapter = MyAdapter(this@MainActivity,it)
recycle.adapter= adapter
}
Log.d("goo",response.body().toString())
}
override fun onFailure(call: Call<List<MydatsItem>>, t: Throwable) {
Log.d("goo",t.message.toString())
}
})
}
}
interface MyData{
@GET("/kicker-music/wordpress/wp-json/kicker-api/get-cat")
fun getData(): Call<List<MydatsItem>>
}
in my adapter class file
class MyAdapter(val context:Context, val Items:List<MydatsItem>):RecyclerView.Adapter<MyAdapter.MyViewholder>() {
class MyViewholder(itemView:View):RecyclerView.ViewHolder(itemView){
val catName = itemView.findViewById<TextView>(R.id.txt)
val image = itemView.findViewById<ImageView>(R.id.img)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewholder {
val v = LayoutInflater.from(context).inflate(R.layout.activity_categary,parent,false)
return MyViewholder(v)
}
override fun getItemCount(): Int {
return Items.size
}
override fun onBindViewHolder(holder: MyViewholder, position: Int) {
holder.catName.text = Items[position].cat_name
Picasso.get().load(Items[position].image).into(holder.image);
}
}
here is my data class
data class MydatsItem(
val cat_name: String,
val image: String
)
i Want to load image in my recycler view using Picasso.
2
Answers
i tried using Glide library like below.
it still dosn't display image.
my gradle file is below
my Adapter class is here