英文:
SpannableStringBuilder not display image span for every data from Firebase (Kotlin)
问题
这是输出:
我希望每个数据都有一张图片。
这是我的代码:
val database = FirebaseDatabase.getInstance().reference
val getData = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val myData = SpannableStringBuilder()
for (i in snapshot.children) {
val data = i.child("myData").value
myData.append("$data\n\n")
// 从drawable获取图像并设置大小
val drawable: Drawable? = context?.let { ContextCompat.getDrawable(it, R.drawable.myImage) }
drawable?.setBounds(0, 0, textView.lineHeight, textView.lineHeight)
// 将图像span设置为第一个字母
val span: ImageSpan? = drawable?.let { ImageSpan(it, ImageSpan.ALIGN_BASELINE) }
myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
textView.text = myData
}
override fun onCancelled(error: DatabaseError) {
}
}
database.addValueEventListener(getData)
英文:
I retrieve the data from FireBase
and trying to display an image next to each data (TextView
). But the image is displayed on the first data only.
This is the output:
I want every data to have an image.
Here is my code:
val database = FirebaseDatabase.getInstance().reference
val getData = object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val myData = SpannableStringBuilder()
for (i in snapshot.children) {
val data = i.child("myData").value
myData.append("$data\n\n")
// Get image from drawable and set size
val drawable: Drawable? = context?.let { ContextCompat.getDrawable(it, R.drawable.myImage) }
drawable?.setBounds(0, 0, textView.lineHeight, textView.lineHeight)
// Set image span to the first letter
val span: ImageSpan? = drawable?.let { ImageSpan(it, ImageSpan.ALIGN_BASELINE) }
myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
}
textView.text = myData
}
override fun onCancelled(error: DatabaseError) {
}
}
database.addValueEventListener(getData)
答案1
得分: 1
问题出在 myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
。 你总是在整个字符串的开头设置图像跨度。 做一些类似这样的事情:
override fun onDataChange(snapshot: DataSnapshot) {
val builder = SpannableStringBuilder()
snapshot.children.forEach { data ->
builder.append("$data#") // the `#` doesn't mind and will be replaced with the image span
val span = context?.let {
ImageSpan(it, R.drawable.myImage, DynamicDrawableSpan.ALIGN_BASELINE)
}
builder.setSpan(
span,
builder.length - 1,
builder.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
builder.append("\n\n") // at the end, append new lines
}
textView.text = builder
}
英文:
The problem is at myData.setSpan(span, 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE)
.
You always set the image span at the first of the whole string. Do something like this:
override fun onDataChange(snapshot: DataSnapshot) {
val builder = SpannableStringBuilder()
snapshot.children.forEach { data ->
builder.append("$data#") // the `#` doesn't mind and will be replaced with the image span
val span = context?.let {
ImageSpan(it, R.drawable.myImage, DynamicDrawableSpan.ALIGN_BASELINE)
}
builder.setSpan(
span,
builder.length - 1,
builder.length,
Spanned.SPAN_EXCLUSIVE_EXCLUSIVE
)
builder.append("\n\n") // at the end, append new lines
}
textView.text = builder
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论