简体   繁体   English

打开卡片视图时如何获取特定用户的数据

[英]How to get specific user's data when open a card view

I search a user based on the name or email and click on that card view.我根据姓名或电子邮件搜索用户,然后单击该卡片视图。 I want the screen to display the user's data from firebase.我希望屏幕显示来自 firebase 的用户数据。 How do I get that user's Id based on that?我如何基于此获取该用户的 ID? I used a bundle but I am getting an error.我使用了一个捆绑包,但出现错误。

Here is my code for the individual page.这是我的单个页面的代码。

public class UserSingleActivity extends AppCompatActivity {
    private Button btnsendFriendRequest;
    private TextView displayName,displayEmail,displayId;
    private DatabaseReference mUserDatabase;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_single);

        //.....

        //Here is the error
        String userId=getIntent().getExtras().getString("userId");//look for the user Id

        mUserDatabase= FirebaseDatabase.getInstance().getReference().child("users").child(userId);

        mUserDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String display_name=dataSnapshot.child("name").getValue().toString();
                String display_email=dataSnapshot.child("email").getValue().toString();
                displayName.setText(display_name);
                displayEmail.setText(display_email);
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }
}

Here is the viewHolder.这是viewHolder。

public class FriendsViewHolders extends RecyclerView.ViewHolder implements View.OnClickListener {

    private DatabaseReference userDB;
    public TextView name,email,userId;

    public FriendsViewHolders(@NonNull View itemView) {
        super(itemView);
        itemView.setOnClickListener(this);

        name = (TextView)itemView.findViewById(R.id.name);
        email = (TextView)itemView.findViewById(R.id.email);

    }
    @Override
    public void onClick(View v) {
        Intent intent=new Intent(v.getContext(), UserSingleActivity.class);
        v.getContext().startActivity(intent);

    }
}

Here is the error.这是错误。

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference
        at com.example.android.myapplication.UserSingleActivity.onCreate(UserSingleActivity.java:29)

You are trying to access a value that hasn't been previously saved in the bundle.您正在尝试访问以前未保存在包中的值。 Before starting the activity, you should use something like:在开始活动之前,您应该使用以下内容:

intent.putExtra("userId", name.getText().toString());

Or instead of name.getText().toString(), you should put the name you will load from the other class.或者,您应该输入将从其他类加载的名称,而不是 name.getText().toString()。 This would save the value and you should be able to access it later.这将保存该值,您应该可以稍后访问它。

Hope it helps!希望能帮助到你!

mUserDatabase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                String display_name=dataSnapshot.child("name").getValue().toString();
                String display_email=dataSnapshot.child("email").getValue().toString();
                displayName.setText(display_name);
                displayEmail.setText(display_email);
            }

from above code when you're doing当你在做的时候从上面的代码

String display_name=dataSnapshot.child("name").getValue().toString();
                String display_email=dataSnapshot.child("email").getValue().toString();

send this to other classAdapter where you're putting to show.将此发送到您要展示的其他 classAdapter。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM