Search code examples
androidandroid-adapterandroid-glidebaseadapterandroid-gridview

can't show image in my gridView by adding glide


I am trying to create a gridview with some cardview containing imageview, and I am setting the default image in the adapter getView method but it is not working.

This is my gridView Adapter:

class ImageAdapter constructor(private val mContext: Context, private val images: Array<Int>) :  BaseAdapter() {
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        var convertView = convertView

        if(convertView == null) {
            val layoutInflater = LayoutInflater.from(mContext)
            convertView = layoutInflater.inflate(R.layout.card_view, null)
        }
        val image = convertView!!.findViewById<ImageView>(R.id.added_picture)

        Glide.with(mContext).load(images[position]).into(image)

        return convertView
    }

    override fun getItem(position: Int): Any? {
        return images[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        return images.size
    }

}

The layout I'm adding in the adapter :

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="110dp"
  android:layout_height="110dp"
  android:layout_margin="3dp"
  app:layout_columnSpan="1"
  app:cardCornerRadius="8dp"
  app:layout_rowSpan="1"
  app:layout_column="2"
  app:layout_row="0" >
  <ImageView
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/added_picture"
    />
</androidx.cardview.widget.CardView>

The MainActivity.kt onCreate method:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        gridView.adapter = ImageAdapter(this, arrayOf(R.drawable.filled_rectangle, R.drawable.filled_rectangle))
}

My gridView is showing only empty space, if I do not use


Solution

  • Glide.with(this).load(getImage(my_drawable_image_name)).into(myImageView);
    
    public int getImage(String imageName) {
    
    int drawableResourceId = this.getResources().getIdentifier(imageName, "drawable", this.getPackageName());
    
    return drawableResourceId;
    }
    

    or You can use imageview.setImageResource(images[postion])

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
    
    <androidx.cardview.widget.CardView
        android:layout_width="110dp"
        android:layout_height="110dp"
        app:cardCornerRadius="8dp"
        android:layout_marginTop="10dp"
        app:layout_column="2"
        app:layout_columnSpan="1"
        app:layout_row="0"
        app:layout_rowSpan="1">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/dp_20" />
    
    </androidx.cardview.widget.CardView>