从Firebase数据库检索Uid

时间:2018-12-10 15:30:22

标签: android firebase firebase-realtime-database

我目前正在使用聊天应用程序。我的目标是在单击该用户名时获取特定的ID(在图片中标记),并将其显示在日志中。

此图像描述了要检索的值。 enter image description here

这是回收站View的ViewHolder类和Adapter类。

public class UsersAdapterClass extends RecyclerView.Adapter<UsersAdapterClass.UsersViewHolder>{
    Context context;
    List<Users> usersList;
    FirebaseDatabase firebaseDatabase;
    DatabaseReference db;
    public UsersAdapterClass(Context context, List<Users> usersList) {
        this.context = context;
        this.usersList = usersList;
    }

    @Override
    public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.users_single_layout,parent,false);
        UsersViewHolder usersViewHolder = new UsersViewHolder(view);
        return usersViewHolder;
    }

    @Override
    public void onBindViewHolder(UsersViewHolder holder, int position) {
        Users users =  usersList.get(position);
        holder.name.setText(users.getName());
        holder.status.setText(users.getStatus());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                firebaseDatabase= FirebaseDatabase.getInstance();
                db = firebaseDatabase.getInstance().getReference("Users");
                final String firebaseId2 = db.push().getKey();
                Log.d("User ID",firebaseId2);
            }
        });

    }

    @Override
    public int getItemCount() {
        return usersList.size();
    }

    public class UsersViewHolder extends RecyclerView.ViewHolder{

        CircleImageView image;
        TextView name,status;

        public UsersViewHolder(View itemView) {
            super(itemView);
            image = itemView.findViewById(R.id.circleImageView);
            name = itemView.findViewById(R.id.users_displayname);
            status = itemView.findViewById(R.id.users_status);
        }

        public CircleImageView getImage() {
            return image;
        }
    }
}

这也是保存回收者视图的类的代码。

public class UsersMainActivity extends AppCompatActivity {

    private Toolbar mToolbar;
    RecyclerView mRecyclerView;
    UsersAdapterClass adapter;
    private DatabaseReference mUserDatabase;
    List<Users> usersList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_users_main);
        mToolbar = findViewById(R.id.allusers_appbar);
        mRecyclerView = findViewById(R.id.allusers_recyclerView);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("All users");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users");

        usersList = new ArrayList<>();

        DatabaseReference dbUsers = FirebaseDatabase.getInstance().getReference("Users");
        dbUsers.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                if(dataSnapshot.exists())
                {
                    for(DataSnapshot userSnapshot:dataSnapshot.getChildren())
                    {
                        Users u = userSnapshot.getValue(Users.class);
                        usersList.add(u);
                    }
                    adapter = new UsersAdapterClass(UsersMainActivity.this,usersList);
                    mRecyclerView.setAdapter(adapter);

                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });    
    }
}

2 个答案:

答案 0 :(得分:0)

根据数据库的结构,当您遍历接收到的快照时,用户ID将成为密钥。要获取密钥,只需在循环的数据快照上调用getKey()。为此,您可以在循环之间添加以下代码,然后将其记录到Logcat中。

String userIds = userSnapshot.getKey(); //Get keys
Log.d("USERID KEYS", userIds) //Log to logcat

编辑:为您的User模型/ POJO中的用户ID创建 getter setter 。然后将userIds传递给设置器。例如u.setUserId(userIds)。现在,在适配器中,只需将userid中的onclick()

...
     @Override
     public void onClick(View v) {

         Log.d("User ID",user.getUserId());
    }
...

答案 1 :(得分:0)

在您的 onBindViewHolder 中,您可以通过这种方式将 uid 存储在字符串中

final String userIds = getRef(position).getKey();