Create Note with Userid firebase android studio

▼魔方 西西 提交于 2020-01-16 19:31:12

问题


Hello guys in this application i use firebase as backend where i have created users with firebase auth. i want to Add the firebase Auth id in the note so i can retrive notes to that current logged in user. right now my notes gets added into database under (ToDo) however they get their own id and not the user id how can i fix this ?

    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:


Your current code pushes new ToDoList objects to /toDo.

So you can link a user to that ToDoList before using it with setValue using something similar to:

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

Alternatively, you can nest the ToDoList objects under the user ID using:

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

Note: Don't forget to handle cases where your active user is not logged in.



来源:https://stackoverflow.com/questions/59495743/create-note-with-userid-firebase-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!