How to store a users UID in a firebase database and have it map to a POJO

后端 未结 2 913
执笔经年
执笔经年 2021-02-08 16:12

CONTEXT

I am currently working on a Firebase application (in Android). I have set up authentication, storage and the database with no issues to speak of. As the Firebase

2条回答
  •  别跟我提以往
    2021-02-08 16:57

    I had the same problem while using FirebaseRecyclerAdapter, so I copied the code and added the following method since the adapter stores the snapshots.

    public String getItemKey(int position) {
        return mSnapshots.getItem(position).getKey();
    }
    

    Now you can just set the user's UID using a setter.

    EDIT : I was thinking something along these lines

    User.java

    public class User {
    
        private String name;
        private String email;
        private String UID;
    
        public User() {
        }
    
        public String getUID() {
        return UID;
        }
    
        public void setUID(String UID) {
        this.UID = UID;
        }
    
        public String getEmail() {
        return email;
        }
    
        public void setEmail(String email) {
        this.email = email;
        }
    
        public String getName() {
        return name;
        }
    
        public void setName(String name) {
        this.name = name;
        }
    }
    

    And in your Firebase adapter's populateView()

    String UID;
    
    FirebaseListAdapter searchAdapter = new FirebaseListAdapter(getActivity(), User.class, R.layout.user_layout, mRef) {
            @Override
            protected void populateView(View v, User model, int position) {
                // If you really need to set the UID here              
                // model.setUID = getRef(position).getKey();
                // Otherwise, I would just set a String field as shown 
                //and pass it with the intent to get the UID 
                //in the profile Activity
                UID = getRef(position).getKey
                ((TextView) v.findViewById(R.id.text1)).setText(model.getName());
                ((TextView) v.findViewById(R.id.text2)).setText(model.getEmail());
                    v.setOnClickListener(MainActivity.this);
            }
        };
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(this, ProfileActivity.class);
            intent.putStringExtra(UID_EXTRA, UID);
            startActivity(intent);
        }
    

提交回复
热议问题