尽管接收到快照并在ArrayList
onDataChange()
函数中添加了数据,但此函数仍然没有返回任何数据,但最终,它返回的ArrayList
大小为0。
List<ProductEntity> feed_data() {
final List<ProductEntity> feededProducts = new ArrayList<>();
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference("/products");
//adding an event listener to fetch values
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//dismissing the progress dialog
progressDialog.dismiss();
//iterating through all the values in database
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ProductEntity upload = postSnapshot.getValue(ProductEntity.class);
Log.d("error", "onDataChange: " + upload.about);
feededProducts.add(upload);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Error", "onCancelled: " + databaseError.getMessage());
NestedScrollView root_layout = findViewById(R.id.category_root_layout) ;
Snackbar.make(root_layout, "Internal Error!!", Snackbar.LENGTH_SHORT).show();
}
});
return feededProducts ;
}
答案 0 :(得分:3)
onDataChange(DataSnapshot快照)是异步调用的。当我们从Firebase获取数据时,就会调用它。
因此,在onDataChange(DataSnapshot snapshot)方法之前调用返回feededProducts,因此每次都会返回空列表。
你必须在onDataChange(DataSnapshot snapshot)内的适配器上调用notifydatasetchanged
答案 1 :(得分:1)
您需要创建一个接收数据的接口。
interface DataReceivedListener{
void onDataReceived(List<ProductEntity> data);
}
void feed_data(DataReceivedListener listener) {
final List<ProductEntity> feededProducts = new ArrayList<>();
progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Please wait...");
progressDialog.show();
mDatabase = FirebaseDatabase.getInstance().getReference("/products");
//adding an event listener to fetch values
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
//dismissing the progress dialog
progressDialog.dismiss();
//iterating through all the values in database
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ProductEntity upload =
postSnapshot.getValue(ProductEntity.class);
Log.d("error", "onDataChange: " + upload.about);
feededProducts.add(upload);
}
listener.onDataReceived(feededProducts);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.d("Error", "onCancelled: " + databaseError.getMessage());
NestedScrollView root_layout =
findViewById(R.id.category_root_layout) ;
Snackbar.make(root_layout, "Internal Error!!",
Snackbar.LENGTH_SHORT).show();
}
});
}
在Activty中实现DataReceivedListener接口并覆盖'onDataReceived()'函数并将'this'传递给'feed_data(DataReceivedListener listener)'或创建DataReceivedListener作为变量并将该变量传递给'feed_data(DataReceivedListener listener) )'
当在'onDataChange(DataSnapshot snapshot)'中接收到数据时,它会调用接口的函数,然后调用被覆盖的函数或者初始化'DataReceivedListener'变量的函数。取决于你是如何做到的。
将代码放在“onDataReceived(列表数据)”函数中需要数据的位置。
答案 2 :(得分:1)
我需要在我的Arralist中添加所有数据之后初始化并设置适配器,如此指出above这样就解决了我的问题:
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
ProductEntity upload = postSnapshot.getValue(ProductEntity.class);
productList.add(upload);
}
productsRecyclerAdapter = new ProductsRecyclerAdapter(productList, CategoryActivity.this);
products.setAdapter(productsRecyclerAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
NestedScrollView root_layout = findViewById(R.id.category_root_layout);
Snackbar.make(root_layout, "Internal Error!!", Snackbar.LENGTH_SHORT).show();
}
});