Im trying to load images in a RecyclerView element:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardCornerRadius="20dp"
android:id="@+id/cardview"
android:layout_marginTop="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/innerrl"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:layout_marginStart="20dp"
android:id="@+id/thumbnail"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:id="@+id/name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/desc"
android:layout_toEndOf="@+id/thumbnail"
android:layout_marginStart="20dp"
android:layout_below="@+id/name"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</RelativeLayout>
RecyclerView adapter:
class RecyclerAdapter : RecyclerView.Adapter<RecyclerAdapter.ViewHolder>() {
lateinit var ctx:Context
lateinit var superHeroes:List<SuperHeroe>
fun RecyclerAdapter(listaHeroes:List<SuperHeroe>, ctx:Context){
this.superHeroes=listaHeroes
this.ctx=ctx
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val layoutInflater = LayoutInflater.from(parent.context)
return ViewHolder(layoutInflater.inflate(R.layout.superhero_layout, parent, false))
}
override fun onBindViewHolder(holder: RecyclerAdapter.ViewHolder, position: Int) {
val item = superHeroes.get(position)
holder.bind(item, ctx)
}
override fun getItemCount(): Int {
return superHeroes.size
}
class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
val superheroName = view.findViewById(R.id.name) as TextView
val desc = view.findViewById(R.id.desc) as TextView
val avatar = view.findViewById(R.id.thumbnail) as ImageView
fun bind(superhero:SuperHeroe, context: Context){
superheroName.text = superhero.nombre
desc.text = superhero.desc
loadUrl(superhero.imagen, avatar, context)
}
fun loadUrl(url: String, target:ImageView, ctx:Context) {
println(url)
Picasso.with(ctx).load(url).into(target)
}
}
}
The version of Picasso is:
implementation 'com.squareup.picasso:picasso:2.5.2'
No images are loaded in the ImageView, but I've put a log in the loadUrl function, and if I click in the link, the image is shown in my browser. The rest of elements (Name, and description) are shown properly. What I am doing wrong?
Thank you.
are you define internet permission in the manifest? like this,
<uses-permission android:name="android.permission.INTERNET" />
or also check Network security configuration:-
you can try with any options
1. try hitting the URL with "https://" instead of "http://"
2. Create file res/xml/network_security_config.xml -
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
</domain-config>
</network-security-config>
add this file in under application tag in manifest like this,
<application
android:networkSecurityConfig="@xml/network_security_config">
</application>
3. use usesCleartextTraffic
add this under application tag in manifest like this,
<application
android:usesCleartextTraffic="true">
</application>