无法 post Picasso 图像进入 Kotlin 中的 recyclerview viewholder
Cannot post Picasso image into recyclerview viewholder in Kotlin
我目前正在使用 Kotlin 开发一个项目,我无法通过 Picasso 将图像处理到 ViewHolder 中,并且在进行故障排除时不断收到创建扩展函数的请求。这就是问题所在
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val user = mUser[position]
holder.userNameTextView.text = user.getUsername()
holder.userFullNameTextView.text = user.getFullName()
Picasso.get().load(user.getImage())
.placeholder(R.drawable.ic_user_avatar_light)
.into(holder.userProfileImageView)
...我的适配器的其余部分就是这个
class SearchAdapter(private var mContext: Context,
private var mUser: List<User>,
private var isFragment: Boolean = false) : RecyclerView.Adapter<SearchAdapter.ViewHolder>() {
private var firebaseUser: FirebaseUser? = FirebaseAuth.getInstance().currentUser
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(mContext).inflate(R.layout.user_item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val user = mUser[position]
holder.userNameTextView.text = user.getUsername()
holder.userFullNameTextView.text = user.getFullName()
Picasso.get().load(user.getImage())
.placeholder(R.drawable.ic_user_avatar_light)
.into(holder.userProfileImageView)
user.getUid()?.let { checkWatchingStatus(it, holder.userWatchButton as Button) }
holder.itemView.setOnClickListener{
val pref = mContext.getSharedPreferences("PREFS", Context.MODE_PRIVATE).edit()
pref.putString("profileID", user.getUid())
pref.apply()
(mContext as FragmentActivity).supportFragmentManager.beginTransaction()
.replace(R.id.viewPager, ProfileFragment()).commit()
}
holder.userWatchButton.setOnClickListener{
if(holder.userWatchButton.text.toString() == "Watch"){
firebaseUser?.uid.let{ itl ->
user.getUid()?.let { it1 ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(itl.toString())
.child("Watching").child(it1)
.setValue(true).addOnCompleteListener{ task ->
if (task.isSuccessful){
firebaseUser?.uid.let{ itl ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(user.getUid()!!)
.child("Watchers").child(itl.toString())
.setValue(true).addOnCompleteListener{ task ->
if (task.isSuccessful){
}
}
}
}
}
}
}
} else {
firebaseUser?.uid.let{ itl ->
user.getUid()?.let { it1 ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(itl.toString())
.child("Watching").child(it1)
.removeValue().addOnCompleteListener{ task ->
if (task.isSuccessful){
firebaseUser?.uid.let{ itl ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(user.getUid()!!)
.child("Watchers").child(itl.toString())
.removeValue().addOnCompleteListener{ task ->
if (task.isSuccessful){
}
}
}
}
}
}
}
}
}
}
override fun getItemCount(): Int {
return mUser.size
}
class ViewHolder(@NonNull itemView: View) :RecyclerView.ViewHolder(itemView) {
var userNameTextView: TextView = itemView.findViewById(R.id.userName_search)
var userFullNameTextView: TextView = itemView.findViewById(R.id.userFullName_search)
var userProfileImageView: TextView = itemView.findViewById(R.id.userImage_search)
var userWatchButton: TextView = itemView.findViewById(R.id.watchButton_search)
}
private fun checkWatchingStatus(uid: String, userWatchButton: Button){
val watchingRef = firebaseUser?.uid.let{ itl ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(itl.toString())
.child("Watching")
}
watchingRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
if (p0.child(uid).exists()) {
userWatchButton.text = "Watching"
} else {
userWatchButton.text = "Watch"
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
}
有人可以帮忙吗?
在您的 ViewHolder
中,您将 userProfileImageView
声明为 TextView
而不是 ImageView
,并将其传递给 Picasso's
into()
方法,它接受一个 ImageView
作为参数,这就是它要求创建扩展函数的原因。
class ViewHolder(@NonNull itemView: View) :RecyclerView.ViewHolder(itemView) {
var userNameTextView: TextView = itemView.findViewById(R.id.userName_search)
var userFullNameTextView: TextView = itemView.findViewById(R.id.userFullName_search)
//declare userProfileImageView as an ImageView
var userProfileImageView: ImageView = itemView.findViewById(R.id.userImage_search)
var userWatchButton: TextView = itemView.findViewById(R.id.watchButton_search)
}
我目前正在使用 Kotlin 开发一个项目,我无法通过 Picasso 将图像处理到 ViewHolder 中,并且在进行故障排除时不断收到创建扩展函数的请求。这就是问题所在
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val user = mUser[position]
holder.userNameTextView.text = user.getUsername()
holder.userFullNameTextView.text = user.getFullName()
Picasso.get().load(user.getImage())
.placeholder(R.drawable.ic_user_avatar_light)
.into(holder.userProfileImageView)
...我的适配器的其余部分就是这个
class SearchAdapter(private var mContext: Context,
private var mUser: List<User>,
private var isFragment: Boolean = false) : RecyclerView.Adapter<SearchAdapter.ViewHolder>() {
private var firebaseUser: FirebaseUser? = FirebaseAuth.getInstance().currentUser
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(mContext).inflate(R.layout.user_item_layout, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val user = mUser[position]
holder.userNameTextView.text = user.getUsername()
holder.userFullNameTextView.text = user.getFullName()
Picasso.get().load(user.getImage())
.placeholder(R.drawable.ic_user_avatar_light)
.into(holder.userProfileImageView)
user.getUid()?.let { checkWatchingStatus(it, holder.userWatchButton as Button) }
holder.itemView.setOnClickListener{
val pref = mContext.getSharedPreferences("PREFS", Context.MODE_PRIVATE).edit()
pref.putString("profileID", user.getUid())
pref.apply()
(mContext as FragmentActivity).supportFragmentManager.beginTransaction()
.replace(R.id.viewPager, ProfileFragment()).commit()
}
holder.userWatchButton.setOnClickListener{
if(holder.userWatchButton.text.toString() == "Watch"){
firebaseUser?.uid.let{ itl ->
user.getUid()?.let { it1 ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(itl.toString())
.child("Watching").child(it1)
.setValue(true).addOnCompleteListener{ task ->
if (task.isSuccessful){
firebaseUser?.uid.let{ itl ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(user.getUid()!!)
.child("Watchers").child(itl.toString())
.setValue(true).addOnCompleteListener{ task ->
if (task.isSuccessful){
}
}
}
}
}
}
}
} else {
firebaseUser?.uid.let{ itl ->
user.getUid()?.let { it1 ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(itl.toString())
.child("Watching").child(it1)
.removeValue().addOnCompleteListener{ task ->
if (task.isSuccessful){
firebaseUser?.uid.let{ itl ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(user.getUid()!!)
.child("Watchers").child(itl.toString())
.removeValue().addOnCompleteListener{ task ->
if (task.isSuccessful){
}
}
}
}
}
}
}
}
}
}
override fun getItemCount(): Int {
return mUser.size
}
class ViewHolder(@NonNull itemView: View) :RecyclerView.ViewHolder(itemView) {
var userNameTextView: TextView = itemView.findViewById(R.id.userName_search)
var userFullNameTextView: TextView = itemView.findViewById(R.id.userFullName_search)
var userProfileImageView: TextView = itemView.findViewById(R.id.userImage_search)
var userWatchButton: TextView = itemView.findViewById(R.id.watchButton_search)
}
private fun checkWatchingStatus(uid: String, userWatchButton: Button){
val watchingRef = firebaseUser?.uid.let{ itl ->
FirebaseDatabase.getInstance().reference
.child("Watch").child(itl.toString())
.child("Watching")
}
watchingRef.addValueEventListener(object : ValueEventListener {
override fun onDataChange(p0: DataSnapshot) {
if (p0.child(uid).exists()) {
userWatchButton.text = "Watching"
} else {
userWatchButton.text = "Watch"
}
}
override fun onCancelled(p0: DatabaseError) {
}
})
}
}
有人可以帮忙吗?
在您的 ViewHolder
中,您将 userProfileImageView
声明为 TextView
而不是 ImageView
,并将其传递给 Picasso's
into()
方法,它接受一个 ImageView
作为参数,这就是它要求创建扩展函数的原因。
class ViewHolder(@NonNull itemView: View) :RecyclerView.ViewHolder(itemView) {
var userNameTextView: TextView = itemView.findViewById(R.id.userName_search)
var userFullNameTextView: TextView = itemView.findViewById(R.id.userFullName_search)
//declare userProfileImageView as an ImageView
var userProfileImageView: ImageView = itemView.findViewById(R.id.userImage_search)
var userWatchButton: TextView = itemView.findViewById(R.id.watchButton_search)
}