从Firebase实时数据库Android中检索特定数据

时间:2020-04-03 08:25:14

标签: android firebase firebase-realtime-database

从数据库中我只想显示bsc 3流,而不是所有流详细信息。

UserHelperclass是模型类。

此代码显示所有用户:

Screenshot of a tree-like database structure

usersList = new ArrayList<>();

final my_class_adapter class_adapter = new my_class_adapter(my_class.this, usersList);
recyclerView.setAdapter(class_adapter);

databaseReference = FirebaseDatabase.getInstance().getReference("UsersData");

usersListener = databaseReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        usersList.clear();

        for (DataSnapshot itemSnapshot : dataSnapshot.getChildren()) {
            UserHelperClass userHelperClass = itemSnapshot.getValue(UserHelperClass.class);
            usersList.add(userHelperClass);
        }
        class_adapter.notifyDataSetChanged();
    }

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

    }
});

1 个答案:

答案 0 :(得分:0)

// define the object
UserHelperclass user_class;
ArrayList<UserHelperclass> dataList=new ArrayList()<>;

Query qry = databaseReference.getRoot().child("UsersData").orderByChild("stream").equalTo("Bsc It 3");

        qry.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                for (DataSnapshot dataSanpshot1 : dataSnapshot.getChildren()) {

                    // store data into your model class   
                   user_class=dataSanpshot1.getvalue(UserHelperclass.class);

                   // add model class data to the arraylist
                    dataList.add(user_class);
                 }
               // now set arraylist to your adapter of recyclerview pr gridview or listview

            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(CodeActivity.this, "fail to get data", Toast.LENGTH_SHORT).show();
            }
        });