skip to Main Content

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


  1. Chosen as BEST ANSWER

    i tried using Glide library like below.

        class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val recycle = findViewById<RecyclerView>(R.id.recyclr)
            recycle.layoutManager = LinearLayoutManager(this@MainActivity)
    
    
            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>>
                ) {
                    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>>
    }
    

    it still dosn't display image.


  2. my gradle file is below

    plugins {
        id 'com.android.application'
        id 'org.jetbrains.kotlin.android'
    }
    
    android {
        namespace 'com.example.examplere'
        compileSdk 33
    
        defaultConfig {
            applicationId "com.example.examplere"
            minSdk 24
            targetSdk 33
            versionCode 1
            versionName "1.0"
    
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
    
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
            }
        }
        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
        kotlinOptions {
            jvmTarget = '1.8'
        }
    }
    
    dependencies {
    
        implementation 'androidx.core:core-ktx:1.8.0'
        implementation 'androidx.appcompat:appcompat:1.6.1'
        implementation 'com.google.android.material:material:1.5.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
        implementation 'androidx.core:core-ktx:1.9.0'
        testImplementation 'junit:junit:4.13.2'
        androidTestImplementation 'androidx.test.ext:junit:1.1.5'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
    
        implementation 'com.squareup.retrofit2:retrofit:2.9.0'
        implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
    
        implementation 'com.github.bumptech.glide:glide:4.16.0'
    
    }
    

    my Adapter class is here

    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
            Glide
                .with(context)
                .load(Items[position].image)
                .into(holder.image)
    
    
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search