gpt4 book ai didi

Android firestore RecyclerView 正在加载错误的图像

转载 作者:行者123 更新时间:2023-12-02 12:58:56 25 4
gpt4 key购买 nike

在我的应用程序中,我使用的是 firestore recyclerview,它基本上是从云 firestore 文档中获取图像 url,并且使用 glide 图像库将所有获取的 url 加载到 RecyclerView 中,但是它成功加载了图像,但是当我开始时滚动它会在错误的位置显示图像,我无法弄清楚问题的原因。

这是我的代码:

 private fun initializeData() {
db = FirebaseFirestore.getInstance()
val query: Query

query = db!!.collection("Items")
s = "Items"

val response = FirestoreRecyclerOptions.Builder<FriendsResponse>()
.setQuery(query, FriendsResponse::class.java)
.build()
adapter = object : FirestoreRecyclerAdapter<FriendsResponse, FriendsHolder>(response) {
public override fun onBindViewHolder(holder: FriendsHolder, position: Int, model: FriendsResponse) {
val id = snapshots.getSnapshot(position).id
holder.exname.text = id
db!!.collection("Items").document(id).get().addOnCompleteListener { task ->
if(activity!=null){
val document = task.result
val url = document!!.data!!["imageurl"] as String?
Glide.with(holder.imgmenu.context).load(url).into(holder.imgmenu)

}
}
holder.itemView.setOnLongClickListener {
s = "Items"
docname = (recyclerView!!.findViewHolderForAdapterPosition(position)!!.itemView.findViewById<View>(R.id.menuname) as TextView).text.toString()
false
}
holder.itemView.setOnClickListener { v ->
docname = (recyclerView!!.findViewHolderForAdapterPosition(position)!!.itemView.findViewById<View>(R.id.menuname) as TextView).text.toString()
db!!.collection("Items").document(docname!!).get().addOnCompleteListener { task ->
val document = task.result
val url = document!!.data!!["imageurl"] as String?
val id = document.data!!["Id"].toString()
val dialogBuilder = AlertDialog.Builder(activity)
val inflater = activity!!.layoutInflater
val dialogView = inflater.inflate(R.layout.fragment_itemdet, null)
dialogBuilder.setView(dialogView)

name = dialogView.findViewById(R.id.itemname)
img = dialogView.findViewById(R.id.itemdetimg)
img!!.setOnClickListener { v1 ->
docname = (recyclerView!!.findViewHolderForAdapterPosition(position)!!.itemView.findViewById<View>(R.id.menuname) as TextView).text.toString()
//Toast.makeText(getContext(),docname,Toast.LENGTH_LONG).show();
PickImageDialog.build(PickSetup()).show(activity!!)
val setup = PickSetup()
PickImageDialog.build(setup)
.setOnClick(object : IPickClick {
override fun onGalleryClick() {
val intent = Intent()
intent.type = "image/*"
intent.action = Intent.ACTION_GET_CONTENT
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE)
}

override fun onCameraClick() {
val cameraIntent = Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE)
startActivityForResult(cameraIntent, REQUEST_CAMERA)

}
}).show(activity!!)

}
inst = dialogView.findViewById(R.id.tvinst)
etinst = dialogView.findViewById(R.id.etinst)
counter = dialogView.findViewById(R.id.counter)
add = dialogView.findViewById(R.id.add)
del = dialogView.findViewById(R.id.sub)
confirm = dialogView.findViewById(R.id.confirm)
val custom_font = Typeface.createFromAsset(activity!!.assets, "fonts/Poppins-ExtraBoldItalic.ttf")
name!!.typeface = custom_font
val alertDialog = dialogBuilder.create()
add!!.setOnClickListener { v -> counter!!.text = i++.toString() }
del!!.setOnClickListener { v -> counter!!.text = i--.toString() }
name!!.text = docname

Glide.with(holder.imgmenu.getContext()).load(url).into(img)
confirm!!.setOnClickListener { v ->
val qty = counter!!.text.toString()
val inst = etinst!!.text.toString()
if (qty == "0") {
Toasty.error(getContext()!!, "Please Specify Quantity", Toast.LENGTH_LONG, true).show()
} else {
db = FirebaseFirestore.getInstance()
db!!.collection("Order").document(TableListFragment.tableno)
.update(
"details", FieldValue.arrayUnion("$docname--$qty--$inst")
).addOnCompleteListener {
Toasty.success(getContext()!!, "Item Added", Toast.LENGTH_LONG, true).show()
alertDialog.dismiss()
}.addOnFailureListener { e -> Log.i("WhatdFuck:", e.toString()) }

}
}
alertDialog.setTitle("Order")
alertDialog.setMessage("Enter Order Details")
alertDialog.show()
}

}


}

override fun onCreateViewHolder(group: ViewGroup, i: Int): FriendsHolder {
val view = LayoutInflater.from(group.context)
.inflate(R.layout.menu_item, group, false)

return FriendsHolder(view)
}

override fun onError(e: FirebaseFirestoreException) {
Log.e("error", e.message)
}
}

adapter!!.notifyDataSetChanged()
//int spaceInPixels = 200;
val verticalDecoration = DividerItemDecoration(recyclerView!!.context,
DividerItemDecoration.HORIZONTAL)
val verticalDivider = ContextCompat.getDrawable(activity!!, R.drawable.divider_vertical)
verticalDecoration.setDrawable(verticalDivider!!)
recyclerView!!.addItemDecoration(verticalDecoration)

val horizontalDecoration = DividerItemDecoration(recyclerView!!.context,
DividerItemDecoration.VERTICAL)
val horizontalDivider = ContextCompat.getDrawable(activity!!, R.drawable.divider_horizontal)
horizontalDecoration.setDrawable(horizontalDivider!!)
recyclerView!!.addItemDecoration(horizontalDecoration)
recyclerView!!.adapter = adapter

}

我还尝试了来自谷歌的其他解决方案,例如取消请求并在使用 glide 加载之前将 null 传递给 imageview,但它们不起作用。

这是我的截图:数据库: enter image description here

回收者观点: enter image description here

最佳答案

在适配器中覆盖这两个方法。

@Override
public long getItemId(int position) {
return position;
}

@Override
public int getItemViewType(int position) {
return position;
}

抱歉,我不熟悉 Kotlin。

关于Android firestore RecyclerView 正在加载错误的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55394297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com