问题描述
我在Android应用中使用Firebase ValueEventlistener从数据库中检索一些数据以初始化UI,但是我的问题是我不知道何时完成数据提取,这取决于用户的互联网连接速度,我为侦听器完成了5秒钟的延迟以完成数据提取,但是有时这还不够,并且数据仍然为null,从而给出了null指针异常.我想知道Firebase侦听器何时完成数据提取以能够更新我的UI.
I'm using Firebase ValueEventlistener in an Android app to retreive some data from the database to initialize my UI, but my Problem is that I never know when data fetching is done and it depends on the speed of the user's internet connection, I'm making a delay of 5 seconds for the Listeners to finish fetching data but sometimes it's not enough and data is still null giving a null pointer exception. I want to know when the Firebase Listeners finish fetching data to be able to update my UI .
这是我获取数据的一种方法:
This is one Method where I fetch Data :
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.
Here I try to get the data from the Firebase Listeners in an ArrayList to be able to initialize the UI by passing the ArrayLists to an Adapter of a ListView.
有人对此有解决方案吗?
Anyone has a solution for this please ?
推荐答案
在onDataChanged()
方法内执行所有UI更新,如下所示:
Do all the UI updates inside the onDataChanged()
method as follows:
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();
Please dont forget to put this line marketArrayAdapter.notifyDataSetChanged();
这篇关于如何检测Firebase侦听器是否完成了数据提取?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!