简体   繁体   English

将数据从 firebase 数据库提取到 android 应用程序时使用进度条

[英]Use of progressbar while fetching data from firebase database to an android app

I want to show progress bar on the screen untill all the required data is fetched from firebase database.我想在屏幕上显示进度条,直到从 firebase 数据库中获取所有必需的数据。

How can I use the below code in the fetchMenu() [in MenuFragment.kt]如何在 fetchMenu() [在 MenuFragment.kt] 中使用以下代码

.addOnSuccessListener(OnSuccessListener<Void?> { // Write was successful!
            // ...
            progressBar.visibility = View.GONE
        })
        .addOnFailureListener(OnFailureListener { // Write failed
            // ...
            progressBar.visibility = View.GONE
        })

MenuFragment.kt菜单片段.kt

class MenuFragment : Fragment() {

private var dishList: MutableList<DishModel> = mutableListOf()
private lateinit var myRef: DatabaseReference
lateinit var list: RecyclerView
lateinit var proceedToCartLayout: RelativeLayout
lateinit var addToCartBtn: Button
private var selectedCategory = ""
lateinit var progressLayout: RelativeLayout
lateinit var progressBar: ProgressBar

companion object {
    fun newInstance(): Fragment {
        return MenuFragment()
    }
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    val view = inflater.inflate(R.layout.fragment_menu, container, false)
    //retrieve id
    val bundle = this.arguments
    selectedCategory = bundle!!.getString("CATEGORY_ID")!!
    list = view.findViewById(R.id.recyclerMenu)
    myRef = FirebaseDatabase.getInstance().getReference("Category")
    proceedToCartLayout = view.findViewById(R.id.ProceedToCart)
    addToCartBtn = view.findViewById(R.id.btn_cart)
    progressLayout = view.findViewById(R.id.progressLayout)
    progressBar = view.findViewById(R.id.progressBar)


    return view
}


override fun onResume() {

    if (ConnectionManager().checkConnectivity(activity as Context)) {
        fetchMenu()

    } else {

        val alterDialog = androidx.appcompat.app.AlertDialog.Builder(activity as 
Context)
        alterDialog.setTitle("No Internet")
        alterDialog.setMessage("Connect to internet to continue")
        alterDialog.setIcon(R.drawable.nointernet)
        alterDialog.setPositiveButton("Open Settings") { _, _ ->
            val settingsIntent = Intent(Settings.ACTION_SETTINGS)//open wifi settings
            startActivity(settingsIntent)
        }

        alterDialog.setNegativeButton("Exit") { _, _ ->
            ActivityCompat.finishAffinity(activity as Activity)
        }
        alterDialog.setCancelable(false)

        alterDialog.create()
        alterDialog.show()

    }

    super.onResume()
}

private fun fetchMenu() {


    progressLayout.visibility = View.VISIBLE

    myRef.child(selectedCategory)
        .addValueEventListener(object : ValueEventListener {
            override fun onCancelled(p0: DatabaseError) {
                progressLayout.visibility = View.GONE
                Toast.makeText(context, "$p0", Toast.LENGTH_SHORT).show()
            }


            override fun onDataChange(p0: DataSnapshot) {
                progressLayout.visibility = View.GONE
                if (p0.exists()) {
                    dishList.clear()
                    for (i in p0.children) {
                        val plan = i.getValue(DishModel::class.java)
                        dishList.add(plan!!)
                    }

                    val adapter = MenuAdapter(
                        context!!,
                        R.layout.menu_list_item,
                        dishList,
                        proceedToCartLayout,
                        addToCartBtn, selectedCategory
                    )
                    list.adapter = adapter

                }
            }


        })
}


}

I got the result after using progress bar but didn't get the required result as I want to display the progress bar untill all the rows of the RecyclerView get filled with the data fetched from the firebase database and all the views correctly placed.我在使用进度条后得到了结果,但没有得到所需的结果,因为我想显示进度条,直到 RecyclerView 的所有行都填满了从 firebase 数据库中获取的数据并且所有视图都正确放置。

I'm getting this while using progress bar我在使用进度条时得到了这个

see the video 看视频

According to your shared image, you aren't displaying the "ProgressBar" at all.根据您共享的图像,您根本没有显示“ProgressBar”。 To solve this, please uncomment the following line of code:要解决此问题,请取消注释以下代码行:

progressLayout.visibility = View.VISIBLE

And add the following line:并添加以下行:

progressLayout.visibility = View.GONE

Inside the "onDataChange()" method as well.在“onDataChange()”方法中也是如此。 In this way you are starting to display the ProgressBar when the "fetchMenu()" method is called and once you get a response, either the data or an Exception you can hide it.通过这种方式,当调用“fetchMenu()”方法时,您将开始显示 ProgressBar,一旦获得响应,您可以将数据或异常隐藏起来。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM