且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将用户UID存储在Firebase数据库中并将其映射到POJO

更新时间:2023-01-14 11:23:09

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<User> searchAdapter = new FirebaseListAdapter<User>(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);
    }

相关阅读

技术问答最新文章