android-fragments Firebase实时数据库图像未显示在片段Kotlin类中

htrmnn0y  于 2022-11-14  发布在  Android
关注(0)|答案(1)|浏览(132)

我正在尝试显示Firebase Realtime Database存储中的图像。我以前在以前版本的应用中也这样做过,但不同之处在于我的实现方式。我的适配器和数组列表类完全相同,但我没有使用Activity,而是切换到使用片段。
我所做的基本上是复制我的旧作品并进行适当的修改,这样我就不会遇到错误,但不幸的是我遇到了一些。我的图像从Firebase没有显示在所有,我不知道是什么问题。

适配器类

class AbstractAdapter(private val mContext: Context, private val abstractList: ArrayList<Abstract>) : RecyclerView.Adapter<AbstractAdapter.ViewHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.abstract_image_view, parent, false)
        return ViewHolder(view)
    }

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        Glide.with(mContext)
            .load(abstractList[position].abstract)
            .into(holder.imageView)
    }

    override fun getItemCount(): Int {
        return abstractList.size
    }

    inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var imageView: ImageView = itemView.findViewById(R.id.abstractImageView)

    }

    companion object {
        private const val Tag = "RecyclerView"
    }
}

数据类别

class Abstract {
    var abstract: String? = null

    constructor() {}
    constructor(abstract: String?) {
        this.abstract = abstract
    }
}

将在其中显示图像的片段

class AbstractWallpapers: Fragment(), PurchasesUpdatedListener {

private lateinit var subscribeAbstract: Button
private var billingClient: BillingClient? = null

lateinit var recyclerView: RecyclerView
lateinit var abstractlist: ArrayList<Abstract>

private var recyclerAdapterAbstract: AbstractAdapter? = null
private var myRef3: DatabaseReference? = null

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return inflater.inflate(R.layout.fragment_abstract_wallpaper, container, false)

    recyclerView = requireView().findViewById(R.id.abstract_recyclerView)

}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recyclerView = view.findViewById(R.id.abstract_recyclerView)

    val layoutManager = LinearLayoutManager(requireActivity())
    recyclerView.layoutManager = layoutManager

    recyclerView.setHasFixedSize(true)

    myRef3 = FirebaseDatabase.getInstance().reference
    abstractlist = ArrayList()

    ClearAll()
    GetDataFromFirebase()

    subscribeAbstract = view.findViewById(R.id.abstract_subscribe_btn)

    subscribeAbstract.setOnClickListener {
        subscribeAbstract()

    }

    // Establish connection to billing client
    //check subscription status from google play store cache
    //to check if item is already Subscribed or subscription is not renewed and cancelled
    billingClient = BillingClient.newBuilder(requireActivity()).enablePendingPurchases().setListener(this).build()
    billingClient!!.startConnection(object : BillingClientStateListener {
        override fun onBillingSetupFinished(billingResult: BillingResult) {
            if (billingResult.responseCode == BillingClient.BillingResponseCode.OK) {
                val queryPurchase = billingClient!!.queryPurchases(BillingClient.SkuType.SUBS)
                val queryPurchases = queryPurchase.purchasesList
                if (queryPurchases != null && queryPurchases.size > 0) {
                    handlePurchases(queryPurchases)
                } else {
                    saveSubscribeValueToPref(false)
                }
            }
        }

        override fun onBillingServiceDisconnected() {
            Toast.makeText(requireActivity(), "Service Disconnected", Toast.LENGTH_SHORT).show()
        }
    })

    //item subscribed
    if (subscribeValueFromPref) {
        subscribeAbstract.visibility = View.GONE

    } else {
        subscribeAbstract.visibility = View.VISIBLE

    }
}

// Code related to Firebase 
@SuppressLint("NotifyDataSetChanged")
private fun GetDataFromFirebase() {
    val query: Query = myRef3!!.child("Abstract")
    query.addListenerForSingleValueEvent(object : ValueEventListener {
        @SuppressLint("NotifyDataSetChanged")
        override fun onDataChange(snapshot: DataSnapshot) {
            for (dataSnapshot: DataSnapshot in snapshot.children) {
                val abstract = Abstract()

                abstract.abstract = dataSnapshot.child("abstract").value.toString()
                abstractlist.add(abstract)
            }
            recyclerAdapterAbstract = abstractlist.let { AbstractAdapter(requireActivity(), it) }
            recyclerView.adapter = recyclerAdapterAbstract
            recyclerAdapterAbstract!!.notifyDataSetChanged()
        }

        override fun onCancelled(error: DatabaseError) {}
    })
    if (recyclerAdapterAbstract != null) recyclerAdapterAbstract!!.notifyDataSetChanged()
}

private fun ClearAll() {
    abstractlist.clear()
    abstractlist = ArrayList()
}
vom3gejh

vom3gejh1#

我修复了我的问题。原来我在Firebase实时数据库中的读取规则被设置为false。我的代码工作得很完美。这只是我的一个愚蠢的错误。

相关问题