TB AS
TB AS

Reputation: 37

Problems with Querying data from Firebase Database

I am struggling to Query data from Database. My code is working in a way, but I have to turn the screen off and on my mobile to get the data to show in my ListView.The TextView tvuserstorageis the value I want to use to filter my Database after. I have another Activity where I don't Query, and that is working just fine from the same Database.

Hope someone would see what I am doing wrong.

Here is my code:

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

    databaseUsers = FirebaseDatabase.getInstance("").getReference("Ansatte");
    databaseTools = FirebaseDatabase.getInstance("").getReference("Verktøy");

    mAuth = FirebaseAuth.getInstance();
    user = FirebaseAuth.getInstance().getCurrentUser();
    userID = user.getUid();

    tvuserstorage = (TextView) findViewById(R.id.tv_userstorage);
    svusertoollist = (SearchView) findViewById(R.id.sv_usertoollist);
    lvusertoollist = (ListView) findViewById(R.id.lv_usertoollist);

    usertoollist = new ArrayList<>();
    databaseUsers.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            Users userprofile = snapshot.getValue(Users.class);

            if (userprofile != null) {
                String user = userprofile.getUserStorage();
                tvuserstorage.setText(user);
            }
        }

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

@Override
protected void onStart() {
    super.onStart();

    Query userlist = databaseTools.orderByChild("toolOwnerStorage").equalTo(tvuserstorage.getText().toString());

    userlist.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            usertoollist.clear();

            for (DataSnapshot snapshot1 : snapshot.getChildren()){
                Tools tools = snapshot1.getValue(Tools.class);

                usertoollist.add(tools);
            }

            ToolList adapter = new ToolList(UserToolListActivity.this, usertoollist);
            lvusertoollist.setAdapter(adapter);
        }

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

Here is my log for userID right before I use it:

I/TAG: xOPruRmASVcwlkEwr8Xr2mlCygw2

Here is my Firebase Structure:

"Ansatte" : {
  "xOPruRmASVcwlkEwr8Xr2mlCygw2" : {
   "hasUserStorage" : "Ja",
   "userAdmin" : "Nei",
   "userDivision" : "Elektro",
   "userEmail" : "[email protected]",
   "userName" : "Bobby",
   "userStorage" : "220"
 },
 "Ak2hetTTcRMLlapYHDZnTqUM1Ch2" : {
   "hasUserStorage" : "Nei",
   "userAdmin" : "Nei",
   "userDivision" : "Blikk",
   "userEmail" : "[email protected]",
   "userName" : "Teddy",
   "userStorage" : ""
"Verktøy" : {
  "3" : {
    "toolGroup" : "",
    "toolName" : "Wrench",
    "toolOwner" : "",
    "toolOwnerStorage" : "220",
    "toolQRid" : "3",
    "toolStatus" : "",
    "toolType" : "",
    "toolrenteddate" : ""
  },
  "1" : {
    "toolGroup" : "",
    "toolName" : "Pliers",
    "toolOwner" : "",
    "toolOwnerStorage" : "225",
    "toolQRid" : "1",
    "toolStatus" : "",
    "toolType" : "",
    "toolrenteddate" : "" 

So here is what I want to happen: When the user "xOPruRmASVcwlkEwr8Xr2mlCygw2" is logged in I want to display only if the data from Verktøy and child("toolOwnerStorage) is equal to Ansatte and child("userStorage). And in a way it works with the code I posted, but I have to turn my screen on and off to get it to show. But if I take away the Query it shows everything without any problems.

Upvotes: 0

Views: 65

Answers (1)

TB AS
TB AS

Reputation: 37

I figure this out, hope this is a good solution. I put the Query inside off the addListener. And now it works.

databaseUsers.child(userID).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            Users userprofile = snapshot.getValue(Users.class);

            if (userprofile != null) {

                String user = userprofile.getUserStorage();

                tvuserstorage1.setText(user);

                Query userlist = databaseTools.orderByChild("toolOwnerStorage").equalTo(user);

                userlist.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        toolListData.clear();
                        for (DataSnapshot snapshot1 : snapshot.getChildren()){

                            Tools data = snapshot1.getValue(Tools.class);
                            toolListData.add(data);

                        }


                        sharedToolsAdapter.notifyDataSetChanged();
                    }

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


                //Log.d("TAG", user);
            }

        }

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

        }

    });



}

Upvotes: 1

Related Questions