有没有办法在不获取空变量的情况下从 firebase 实时数据库保存数据?

Is there a way to save data from a firebase Realtime Database without getting a null variable?

我有一个 Firebase 实时数据库,我想检索一些数据并将它们放入数组列表中。

ArrayList<Item> itemsArray;
List<Item> itemsList;

DatabaseReference shopRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_shop);

    itemsArray = new ArrayList<>();

    shopRef = FirebaseDatabase.getInstance().getReference();

    Query query = shopRef.child("shop");

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                Item item = new Item();
                item.setLogo(snapshot.child("Logo").getValue().toString());
                item.setName(snapshot.child("Name").getValue().toString());
                item.setDescription(snapshot.child("Description").getValue().toString());
                itemsArray.add(item);  //itemsArray is full

            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
        }
    });

    itemsList = itemsArray; // itemsArray is null
}

问题是在listener之后,看起来很容易修复,但是我不明白为什么arraylist为null。

数据库正常

项目 class 正常工作

当您将侦听器添加到查询时,您不会立即将数据返回到数组列表中。你所写的意思是:

  • 查询实时数据库,
  • 当接收到数据更改事件时(或添加侦听器时)调用 onDataChanged(DataSnapshot) 函数

所以你需要的是以后获取数据时的回调

这意味着您不会立即获得数据:

itemsList = itemsArray; // itemsArray is null

改为执行以下操作:

class MyActivity extends AppCompatActivity {
   private final List<Item> itemsArrayList = new ArrayList<>();

   @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop);
    
        shopRef = FirebaseDatabase.getInstance().getReference();
    
        Query query = shopRef.child("shop");
    
        query.addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                List<Item> snapshotItems = new ArrayList<>();
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    Item item = new Item();
                    item.setLogo(snapshot.child("Logo").getValue().toString());
                    item.setName(snapshot.child("Name").getValue().toString());
                    item.setDescription(snapshot.child("Description").getValue().toString());

                    snapshotItems.add(item);
                }
                // Calling this function will add the data back to the list
                MyActivity.this.onItemsObtained(snapshotItems);
            }
    
            @Override
            public void onCancelled(@NonNull DatabaseError error) {
            }
        });
    }

    public void onItemsObtained(List<Item> items) {
        itemsArrayList.clear(); // To remove old Data
        itemsArrayList.addAll(items);
        
        // Continue your own logic...
    }

}