【问题标题】:Why the progressDialog is not dismiss when no data is present in firebase RealTime Database?当firebase实时数据库中没有数据时,为什么progressDialog不会被关闭?
【发布时间】:2020-06-14 12:17:57
【问题描述】:

这是我的代码,当 Firebase 上存在数据并提取到 UI 中时,我的 ProgressDialog 会关闭,但是当 Firebase 上没有数据时它仍在处理并且当我们点击屏幕时它会消失,所以我怎样才能解除它?

public void ShowData()//String data)
        {
           final ProgressDialog progressDialog =new ProgressDialog ( this );
            progressDialog.setMessage ( "Loading.." );
            progressDialog.show ();

        query1 = myRef.orderByChild ( "dateTime" ); // this is for Filter the data (The recently data you add is show on top of application)
        //  options=new FirebaseRecyclerOptions.Builder<modelClass> ().setQuery ( myRef,modelClass.class ).build ();//for retrive
        options = new FirebaseRecyclerOptions.Builder < modelClass > ().setQuery ( query1 , modelClass.class ).build ();//for search and retrive
        adapter = new FirebaseRecyclerAdapter < modelClass, MyVIewHolder > ( options ) {

            @Override
            protected void onBindViewHolder(@NonNull final MyVIewHolder holder , final int position , @NonNull modelClass model)  //for binding the View of recycler_view_item_layout
            {

           /* holder.college.setText ( model.getCollege () );
            holder.contact.setText ( model.getContact ()  );
            holder.time.setText ( model.getDateTime ());
            holder.food.setText ( model.getFood () );
            holder.name.setText ( model.getName () );*/



           progressDialog.dismiss ();
                    holder.setFoodPost ( model ); //for set all data to view



                //for set intent on Contact num View
                holder.contact.setOnClickListener ( new View.OnClickListener () {
                    @Override
                    public void onClick(View view) {
                        String phoneNum = holder.contact.getText ().toString ();
                        Intent in = new Intent ( Intent.ACTION_DIAL );
                        in.setData ( Uri.parse ( "tel:" + phoneNum ) );  // This ensures only Dial apps respond
                        if (in.resolveActivity ( getPackageManager () ) != null) {
                            startActivity ( in );

                        } else {
                            Toast.makeText ( MainActivity.this , "Failed" , Toast.LENGTH_SHORT ).show ();
                        }
                    }
                } );


                //for longPress on particular view(CardView)
                //for Delete the particular data when press long click on the View(CardView)
                holder.itemView.setOnLongClickListener ( new View.OnLongClickListener () {
                    @Override
                    public boolean onLongClick(View view) {
                        //for post_key we simply fetch that data postion which we want to delete
                        post_key = getRef ( position ).getKey ();
                        openDeleteWindow ();//this is a method for open the Delete Window
                        // Toast.makeText ( MainActivity.this , "Press" , Toast.LENGTH_SHORT ).show ();
                        return true;
                    }
                } );

【问题讨论】:

  • 如果您没有数据进入绑定视图,请尝试使用日志,您可以轻松找到您的问题

标签: java android firebase firebase-realtime-database firebaseui


【解决方案1】:

FirebaseUI 适配器的onBindViewHolder 为需要在视图中显示的每个项目调用。如果没有与您在视图中显示的引用/查询匹配的数据,那么您的适配器的onBindViewHolder 将永远不会被调用。

因此,如果您使用onBindViewHolder 隐藏progressDialog,则只有在 项时才会隐藏progressDialog。如果没有项目,onBindViewHolder 将不会被调用,progressDialog 将保持可见。

要隐藏 progressDialog 而不管是否有项目,您需要覆盖 FirebaseUI 适配器的 onDataChanged 方法并将其隐藏在那里:

adapter = new FirebaseRecyclerAdapter < modelClass, MyVIewHolder > ( options ) {
    @Override
    public void onDataChanged() {
        progressDialog.hide();
    }

    @Override
    protected void onBindViewHolder(@NonNull final MyVIewHolder holder , final int position , @NonNull modelClass model) {
        ...

由于每次从 Firebase 完全接收到更新时都会调用 onDataChanged,所以当有 数据和没有 数据时都会调用它.

另见:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-04-11
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多