C
C
Chesterfield252021-11-01 15:06:38
Java
Chesterfield25, 2021-11-01 15:06:38

How to correctly create an adapter to receive data from firebase?

I have a fragment in which I need to display user data, which is exactly what it does!

public class ProfileFragment extends Fragment {


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

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

    FragmentProfileBinding binding;

    //Firebase
    private FirebaseAuth mAuth;
    private FirebaseDatabase userDatabase;
    private FirebaseAuth.AuthStateListener listener;
    private FirebaseUser user;
    private DatabaseReference reference;
    private String firebaseId;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        binding = FragmentProfileBinding.inflate(inflater, container, false);

        userDatabase = FirebaseDatabase.getInstance();
        reference = userDatabase.getReference();
        mAuth = FirebaseAuth.getInstance();
        user = mAuth.getCurrentUser();
        firebaseId = user.getUid();

        binding.logOutBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mAuth.signOut();
                startActivity(new Intent(getActivity(), LoginActivity.class));
                getActivity().finish();
            }
        });

        stateListener();
        gettingDatabase();

        // Inflate the layout for this fragment
        return binding.getRoot();
    }

    private void stateListener(){
        listener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                FirebaseUser user = firebaseAuth.getCurrentUser();
                if(user == null){
                    Toast.makeText(getActivity(), "You Need To Login!", Toast.LENGTH_SHORT).show();
                    getActivity().finish();
                }
            }
        };
    }

    private void gettingDatabase(){
        reference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot dataSnapshot : snapshot.getChildren()){

                    User user = new User();
                    user.setPv(dataSnapshot.child(firebaseId).getValue(User.class).getPv());

                    binding.pv.setText(Integer.toString((int) user.getPv()));
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {
                Toast.makeText(getActivity(), "Error" + error.getMessage(), Toast.LENGTH_SHORT).show();

            }
        });
    }

    @Override
    public void onStart() {
        super.onStart();
        mAuth.addAuthStateListener(listener);
    }

    @Override
    public void onStop() {
        super.onStop();
        if(listener != null) {
            mAuth.removeAuthStateListener(listener);
        }
    }
}


But how do I properly create an adapter for this case?
The example https://firebaseopensource.com/projects/firebase/f...
uses RecyclerView and I don't need to use it.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question