使用Userid Firebase Android Studio创建注释

时间:2019-12-27 03:46:26

标签: android firebase firebase-realtime-database

在此应用程序中,大家好,我使用firebase作为后端,在其中我已使用firebase auth创建了用户。我想在笔记中添加firebase身份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);
    }
}

1 个答案:

答案 0 :(得分:1)

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

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

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)

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