如何在显示firebase数据获取的进度条时通过startListening()解决错误? [重复]

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在显示firebase数据获取的进度条时通过startListening()解决错误? [重复]相关的知识,希望对你有一定的参考价值。

这些是我的代码,用于在从firebase获取数据到循环视图时显示进度条。但是onStart(){...}中的startListening()显示错误。我不知道放置这行代码的确切位置。我在MyTask()类中尝试了这个,但它崩溃了应用程序。请检查我的代码并帮助我解决此问题。谢谢

public class Tab1 extends android.support.v4.app.Fragment {

    private RecyclerView mPeopleRV;
    private DatabaseReference mDatabase;
    private FirebaseRecyclerAdapter<News, Tab1.NewsViewHolder> mPeopleRVAdapter;
    ProgressBar pb;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab1, container, false);


        pb = view.findViewById(R.id.pb);
        mPeopleRV = (RecyclerView) view.findViewById(R.id.myRecycleView_tab1);

        mPeopleRV.hasFixedSize();
        mPeopleRV.setLayoutManager(new LinearLayoutManager(this.getActivity()));


        new MyTask().execute();



        return view;

    }

    @Override
    public void onStart() {
        super.onStart();
        mPeopleRVAdapter.startListening();

    }

    @Override
    public void onStop() {
        super.onStop();


    }


    class MyTask extends AsyncTask<Void, Void, Void>{

        @Override
        protected void onPreExecute() {

        }

        @Override
        protected Void doInBackground(Void... voids) {


            pb.setVisibility(View.VISIBLE);
            // FirebaseCode



            mDatabase = FirebaseDatabase.getInstance().getReference().child("Cricket");
            mDatabase.keepSynced(true);

            DatabaseReference personsRef = FirebaseDatabase.getInstance().getReference().child("Cricket");
            Query personsQuery = personsRef.orderByKey();



            FirebaseRecyclerOptions personsOptions = new FirebaseRecyclerOptions.Builder<News>().setQuery(personsQuery, News.class).build();

            mPeopleRVAdapter = new FirebaseRecyclerAdapter<News, NewsViewHolder>(personsOptions) {
                @Override
                protected void onBindViewHolder(NewsViewHolder holder, final int position, final News model) {
                    holder.setTitle(model.getTitle());
                    holder.setDesc(model.getDesc());
                    holder.setImage1(getActivity().getBaseContext(), model.getImage1());
                    holder.setImage2(getActivity().getBaseContext(), model.getImage2());
                    holder.setT1(model.getT1());
                    holder.setT2(model.getT2());
                    holder.setStatus_r(model.getStatus_r());
                    holder.setStatus_g(model.getStatus_g());


                    holder.mView.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            final String url = model.getUrl();
                            final String offers = model.getTarget();

                            if (url == null) {

                                Snackbar snackbar = Snackbar.make(getActivity().findViewById(android.R.id.content), "Will be available soon", Snackbar.LENGTH_LONG);

                                View sbView = snackbar.getView();
                                sbView.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                                snackbar.show();

                            } else {

                                if (offers == null) {

                                    Intent intent = new Intent(getActivity(), WebViewActivity.class);
                                    intent.putExtra("id", url);
                                    startActivity(intent);


                                } else {

                                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                                    startActivity(intent);

                                }


                            }
                        }
                    });
                }

                @Override
                public NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row1, parent, false);

                    return new NewsViewHolder(view);
                }
            };


            mPeopleRV.setAdapter(mPeopleRVAdapter);

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {

            pb.setVisibility(View.GONE);

        }

        @Override
        protected void onCancelled() {
            mPeopleRVAdapter.stopListening();

        }
    }




    public static class NewsViewHolder extends RecyclerView.ViewHolder {
        View mView;

        public NewsViewHolder(View itemView) {
            super(itemView);
            mView = itemView;
        }

        public void setTitle(String title) {
            TextView post_title = (TextView) mView.findViewById(R.id.post_title1);
            post_title.setText(title);
        }

        public void setDesc(String desc) {
            TextView post_desc = (TextView) mView.findViewById(R.id.post_desc1);
            post_desc.setText(desc);
        }

        public void setImage1(Context ctx, String image1) {
            ImageView post_image = (ImageView) mView.findViewById(R.id.post_image1);
            Picasso.with(ctx).load(image1).into(post_image);
        }

        public void setImage2(Context ctx, String image2) {
            ImageView post_image = (ImageView) mView.findViewById(R.id.post_image2);
            Picasso.with(ctx).load(image2).into(post_image);
        }

        public void setT1(String t1) {
            TextView post_t1 = (TextView) mView.findViewById(R.id.team1_name);
            post_t1.setText(t1);
        }

        public void setT2(String t2) {
            TextView post_t2 = (TextView) mView.findViewById(R.id.team2_name);
            post_t2.setText(t2);
        }

        public void setStatus_r(String status_r) {
            TextView status = (TextView) mView.findViewById(R.id.status_upcoming1);
            status.setText(status_r);
        }

        public void setStatus_g(String status_g) {
            TextView status = (TextView) mView.findViewById(R.id.status_available1);
            status.setText(status_g);
        }
    }
}
答案

首先,在xml中创建一个进度条。

<ProgressBar
    android:id="@+id/progressBar1"
    android:layout_marginTop="30dp"
    android:visibility="gone"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true"
    android:layout_width="50dp"
    android:layout_height="50dp"
    />

然后,在你的AsyncTask中:

@Override
    protected String doInBackground(String... params) {

         progressBar.setVisibility(View.VISIBLE);
        // FirebaseCode


        return "this string is passed to onPostExecute";
    }
@Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        progressBar.Visibility(View.GONE);
    }

以上是关于如何在显示firebase数据获取的进度条时通过startListening()解决错误? [重复]的主要内容,如果未能解决你的问题,请参考以下文章

如何获取异步 Firebase 数据读取的进度?

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

尝试从外部脚本更新进度条时,QWidget 变得无响应[重复]

从 firebase 数据库获取数据时显示进度对话框

React + Firebase 存储 + 文件上传和进度显示

将图像上传到 Firebase 存储时如何制作进度条?