【问题标题】:Create Note with Userid firebase android studio使用 Userid firebase android studio 创建注释
【发布时间】:2019-12-27 03:46:26
【问题描述】:

大家好,在这个应用程序中,我使用 firebase 作为后端,我在其中创建了具有 firebase 身份验证的用户。我想在注释中添加firebase Auth id,以便我可以检索该当前登录用户的注释。现在我的笔记被添加到(ToDo)下的数据库中但是他们得到了自己的ID而不是用户ID我该如何解决这个问题?

    public class HomeActivity extends AppCompatActivity {

    private FirebaseAuth firebaseAuth;
    private Button  logout;
    private GridLayoutManager gridLayoutManager;
    private RecyclerView modelNotesList; //https://camposha.info/android-firebase-realtime-database-with-recyclerview/
    private FloatingActionButton addNotePageButton;
    private DatabaseReference notesDbRef;
    private FirebaseDatabase firebaseDatabase;

    private TextView Title,text;




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

        Title = findViewById(R.id.tvNoteTitle);
        text = findViewById(R.id.TvNoteText);
        addNotePageButton = findViewById(R.id.fab_button_addPage);
        firebaseDatabase = FirebaseDatabase.getInstance();

        firebaseAuth = FirebaseAuth.getInstance(); // get inSTACE


        // acsess database and retrive data



        FirebaseDatabase.getInstance().getReference("NoteList").child(firebaseAuth.getCurrentUser().getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            NotesList notelist = dataSnapshot.getValue(NotesList.class);
            Title.setText( notelist.getTitle());
            text.setText(notelist.getText());
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {
                Toast.makeText(HomeActivity.this,databaseError.getCode(),Toast.LENGTH_SHORT).show();
            }
        });











        addNotePageButton.setOnClickListener(new View.OnClickListener() { //  Float button click
            @Override
            public void onClick(View v) {

            Intent intent = new Intent(HomeActivity.this, NoteInputActivity.class);
            startActivity(intent);
            }
        });


    }








    // everything below is used in the menu
    private void Logout(){ // sign out method called in switchcase
        firebaseAuth.signOut();
        finish();
        startActivity(new Intent(HomeActivity.this,MainActivity.class));
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) { //create menu on toolbar
        getMenuInflater().inflate(R.menu.menu,menu); //inflated inside
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) { // handle on click events on items on menu
        switch(item.getItemId()){
            case R.id.logoutMenu:{
                Logout();
                break;
            }
            case R.id.ProfileMenu:{
                startActivity(new Intent(HomeActivity.this,ProfileActivity.class));
                break;
            }
        }

        return super.onOptionsItemSelected(item);
    }
}

【问题讨论】:

    标签: android firebase firebase-realtime-database


    【解决方案1】:

    您当前的代码将新的ToDoList 对象推送到/toDo

    因此,您可以在将用户与 setValue 一起使用之前将其链接到该 ToDoList,使用类似于:

    ToDoList toDo = new ToDoList (notes,prio); // object of class ToDoList in models
    toDo.setUserId(firebaseAuth.getCurrentUser().getUid())
    databaseReference.push().setValue(toDo)
    

    或者,您可以使用以下方法将 ToDoList 对象嵌套在用户 ID 下:

    ToDoList toDo = new ToDoList (notes,prio); // object of class ToDoList in models
    databaseReference.child(firebaseAuth.getCurrentUser().getUid()).push().setValue(toDo)
    

    注意:不要忘记处理活动用户未登录的情况。

    【讨论】:

    • 谢谢这很好...如果我想读出当前登录的笔记我去用户ID然后Todo然后笔记ID?
    • 取决于您使用上述哪个。如果您使用第一个,您将使用FirebaseDatabase.getInstance().ref('toDo').orderByChild('userId').equalTo(firebaseAuth.getCurrentUser().getUid()).addSingleValueEventListener(/* ... */) 获得该用户的所有笔记。如果你使用后者,你可以使用FirebaseDatabase.getInstance().ref('toDo').child(firebaseAuth.getCurrentUser().getUid()).addSingleValueEventListener(/* ... */)
    • 我没有得到任何值,我不知道为什么
    • 当你说你“没有价值”时,snapshot.hasChildren() 是假的吗?此外,您还没有提到您使用的是选项 1 还是 2
    • 您好,感谢您的帮助,我用现在(显示)而不是创建来重新编辑帖子
    猜你喜欢
    • 2018-05-18
    • 2022-10-19
    • 1970-01-01
    • 2017-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-18
    • 2010-10-23
    相关资源
    最近更新 更多