【问题标题】:Asynctask not working with real time database [closed]Asynctask 不适用于实时数据库 [关闭]
【发布时间】:2019-02-25 14:35:22
【问题描述】:

有没有办法在后台从 firebase 实时数据库中获取数据? Asynctask 不能用它。

我想在从数据库中获取数据时显示进度对话框。

 private class asyncData extends AsyncTask<String,String,String> {
        ProgressDialog Asycdialog = new ProgressDialog(getContext());

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            Asycdialog.setMessage("Loading...");
            Asycdialog.show();
        }

        @Override
        protected String doInBackground(String... strings) {
            Query query = databaseReference.child("").orderByChild("cityWithCountry").equalTo(strings[0]);
            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                    arrData.clear();
                    if (dataSnapshot.exists()) {
                        // dataSnapshot is the "issue" node with all children with id 0
                        for (DataSnapshot items : dataSnapshot.getChildren()) {
                            // do something with the individual "issues"
                            Log.d("QueryValues",""+items.getValue());
                            DataConstructor ds = items.getValue(DataConstructor.class);
                            arrData.add(ds);
                        }
                    }
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {

                }
            });


            return  null;
        }
        @Override
        protected void onPostExecute(String s) {
            super.onPostExecute(s);
            Asycdialog.dismiss();
        }
    }

这里是代码。

【问题讨论】:

  • 通常你应该在 pre execute 中显示对话框并在 post execute 中关闭。我不知道为什么这不起作用,请您分享您的代码
  • 你可以使用LiveData做一些后台任务。
  • 你能发布你的AsyncTask吗
  • AsyncTask的贴出代码
  • 这里为什么需要异步任务?

标签: android firebase firebase-realtime-database


【解决方案1】:

试试这个,让我知道它是否有效。

   Asycdialog.show();
     Query query = databaseReference.child("").orderByChild("cityWithCountry").equalTo(strings[0]);
                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        arrData.clear();
                        if (dataSnapshot.exists()) {
                            // dataSnapshot is the "issue" node with all children with id 0
                            for (DataSnapshot items : dataSnapshot.getChildren()) {
                                // do something with the individual "issues"
                                Log.d("QueryValues",""+items.getValue());
                                DataConstructor ds = items.getValue(DataConstructor.class);
                                arrData.add(ds);
**// check condition and hide your dialog here**
                            }
                        }
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

【讨论】:

  • 你的意思是我把 Asycdialog.show() 放在 doInBackground 里面吗?
  • 不,不需要异步任务
  • 感谢它运行良好...
  • 如果您认为有帮助,请接受我的回答并点赞
猜你喜欢
  • 2012-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-28
  • 2018-02-03
  • 1970-01-01
相关资源
最近更新 更多