如何检测Firebase侦听器是否已完成提取数据?

时间:2017-04-06 15:05:20

标签: android firebase firebase-realtime-database

我在Android应用中使用Firebase ValueEventlistener来从数据库中检索一些数据来初始化我的UI,但我的问题是我从来不知道数据提取何时完成并且它取决于用户的速度&# 39;互联网连接,我为听众完成提取数据的延迟时间为5秒,但有时它还不够,数据仍为空,给出空指针异常。我想知道Firebase侦听器何时完成提取数据以便能够更新我的UI。

这是我获取数据的一种方法:

      public void GetRequests(){

         Retrieve.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
     Iterable<DataSnapshot> Games = dataSnapshot.getChildren();


            for(com.google.firebase.database.DataSnapshot game : Games ){

                String Game_Key = game.child("GameKey").getValue(String.class);
                String Game_Location = game.child("Location").getValue(String.class);
                String UserID_Sender = game.child("UserID").getValue(String.class);
                Adapter_Game_Key.add(Game_Key);
                Adapter_UserID_Sender.add(UserID_Sender);
           }
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

在这里,我尝试从ArrayList中的Firebase侦听器获取数据,以便能够通过将ArrayLists传递给ListView的适配器来初始化UI。

任何人都有解决方案吗?

1 个答案:

答案 0 :(得分:3)

执行onDataChanged()方法中的所有UI更新,如下所示:

    ArrayList<Ad> adArrayList_1 = new ArrayList<>();
    DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("ads");

    ListView listView_1 = (ListView) findViewById(R.id.market_list_view_1);

    MarketArrayAdapter marketArrayAdapter = new MarketArrayAdapter(
            this,
            adArrayList_1
    );

    listView_1.setAdapter(marketArrayAdapter);

            ref.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    adArrayList_1.clear();
                    for(DataSnapshot snapshot : dataSnapshot.getChildren()){
                        Ad ad = snapshot.getValue(Ad.class);
                        adArrayList_1.add(ad);

                    }
                    marketArrayAdapter.notifyDataSetChanged();
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });

我的适配器类位于

之下
public MarketArrayAdapter(Context context, ArrayList<Ad> list) {
    super(context, 0, list);
}

@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) {

    final Ad ad = getItem(position);
    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.ad_item, parent, false);
    }
    ImageView imageView = (ImageView) convertView.findViewById(R.id.ads_profile_pic);
    TextView title = (TextView) convertView.findViewById(R.id.ads_title);
    TextView price = (TextView) convertView.findViewById(R.id.ads_price);
    TextView description = (TextView) convertView.findViewById(R.id.ads_short_description);

    byte[] imageAsBytes = Base64.decode(ad.getaPictureUri().getBytes(), Base64.DEFAULT);
    imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
    title.setText(ad.getaTitle());
    price.setText(String.valueOf(ad.getaPrice()));
    description.setText(ad.getaDescription());

    parent.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent in = new Intent(MarketActivity.this, ViewAdActivity.class);
            in.putExtra("key", ad.getaId());
            startActivity(in);
        }
    });
    return convertView;
}

}

请不要忘记这一行marketArrayAdapter.notifyDataSetChanged();