使用Userid Firebase Android Studio创建注释 [英] Create Note with Userid firebase android studio

查看:47
本文介绍了使用Userid Firebase Android Studio创建注释的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此应用程序中,大家好,我使用firebase作为后端,在其中我已使用firebase auth创建了用户.我想在笔记中添加Firebase身份ID,以便可以将笔记检索到该当前登录用户.现在我的笔记被添加到(ToDo)下的数据库中,但是它们得到了自己的ID,而不是用户ID,我该如何解决呢?

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);
    }
}

推荐答案

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

Your current code pushes new ToDoList objects to /toDo.

因此,您可以在用户使用setValue将其链接到ToDoList之前,使用类似于以下内容的方法:

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)

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

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.

这篇关于使用Userid Firebase Android Studio创建注释的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆