android - 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
解决方案
Ensure that your glider loading the correct link to the imageview.
simply toast the loading links to ensure it is correct.
推荐阅读
- ruby-on-rails - 如何将来自不同模型的多个对象合并为 Rails 中的一个对象?
- javascript - 如何以编程方式仅选择用户可选择的所有文本节点
- git - 如何以交互方式将块(差异块)分配给一组新提交?
- ios - 我的 ViewController 中的每个 textField 输入是否都必须有 textField.delegate=self
- python - 如何将函数定义为包入口点?
- python - 如何保存在一段代码中创建的列表,以便在再次使用相同代码时可以再次使用它?
- javascript - 如何使用 javascript 在 safari 中开始全屏显示?
- angular - ng4-Tag-Input 错误:模块 'CVModule' 导入的预期值 'TagInputModule'。请添加@NgModule 注释
- javascript - firestore 两次添加数据并且不让我拥有 UID
- c# - C# listview 子项未显示