0

im am doing my project for studies on android app developing and i have a problem with getting currentUser that is logged in and showing he's name on TextView. it seems that it is always showing me the last user that was created no matter with which user i login

this is the java file:

public class ActionsFragment extends Fragment {

    FirebaseAuth auth;

    Button acManagmentBtn;
    TextView tvName;

    public ActionsFragment() {
        // Required empty public constructor
    }


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);



        DatabaseReference reference = FirebaseDatabase.getInstance().getReference();
        DatabaseReference managerRef = reference.child("Users").child("Manager");
        managerRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {



                    for (DataSnapshot ds : dataSnapshot.getChildren()) {
                        reference.child(FirebaseAuth.getInstance().getCurrentUser().getUid());
                        Manager manager = ds.getValue(Manager.class);
                        tvName.setText(manager.getName());

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
//                Toast.makeText(getActivity(), "Null שם פרטי", Toast.LENGTH_SHORT).show();
            }
        });
        auth = FirebaseAuth.getInstance();


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_actions, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        tvName = view.findViewById(R.id.tv_name);
        acManagmentBtn = view.findViewById(R.id.manage_ac_btn);
        acManagmentBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), AcManagmentActivity.class);
                startActivity(intent);
            }
        });

    }

}

and this is the tree in my firebase realtime database:

link to the database tree

thank you for helping!

Netanel
  • 11
  • 1
  • You're just waiting for a value to be added in the database (`addValueEventListener`) and the you display the name field. Which is why you only get the latest name. – Omar Aflak Jun 09 '20 at 17:57

1 Answers1

0

Firebase reference get always the last user was created

That's the normal behavior since you are setting the name of the manager to the tvName TextView at every iteration of the for loop, meaning that only the last manager name will be set, as it's the last iteration. So bear in mind that a simple TextView it's not the right choice when it comes to data from multiple users, instead, you should use a View that can hold multiple items. A possible solution might be a ListView, or why not, even better a RecyclerView. For that, I recommend you check my answers from the following posts:

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193