【问题标题】:How to connect user to courses and grades realtime database java如何将用户连接到课程和成绩实时数据库java
【发布时间】:2021-02-01 12:31:40
【问题描述】:

在这个项目中,我有两种类型的用户(教师和学生)。老师可以添加班级,学生应该可以选择班级并签到班级,当学生签到班级时,老师应该可以给他打分。我不知道如何将用户连接到课程并给他评分。

这是我的类适配器

public class ClassesAdapter extends FirebaseRecyclerAdapter<Classes, ClassesAdapter.StudentViewHolder> {

    /**
     * Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
     * {@link FirebaseRecyclerOptions} for configuration options.
     *
     * @param options
     */
    public ClassesAdapter(@NonNull FirebaseRecyclerOptions options) {
        super(options);
    }

    protected void onBindViewHolder(@NonNull StudentViewHolder holder, int position, @NonNull Classes model) {
        holder.className.setText(model.getCname());
        holder.classInfo.setText(model.getDescription());
        holder.classUid.setText(model.getUid());
        String keyId = this.getRef(position).getKey();
    }

    @NonNull
    @Override
    public StudentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.classes, parent, false);

        return new ClassesAdapter.StudentViewHolder(view);

    }


    public class StudentViewHolder extends RecyclerView.ViewHolder {
        TextView className;
        TextView classInfo;
        TextView classUid;
        Button classSignInBtn;

        public StudentViewHolder(@NonNull View itemView) {
            super(itemView);
            className = itemView.findViewById(R.id.classNameTxt);
            classInfo = itemView.findViewById(R.id.classInfoTxt);
            classUid = itemView.findViewById(R.id.classUidTxt);
            classSignInBtn = itemView.findViewById(R.id.classSignInBtn);
            int position  = getAdapterPosition();
            classSignInBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FirebaseAuth mAuth = FirebaseAuth.getInstance();
                    String uId = mAuth.getCurrentUser().getUid();
                    FirebaseDatabase db = FirebaseDatabase.getInstance();
                    DatabaseReference ref = db.getReference("ednevnik/korisnici/" + uId);
                    DatabaseReference ref1 = db.getReference("ednevnik/razredi/");

                    ref.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            String name = snapshot.child("name").getValue().toString();
                            String email = snapshot.child("email").getValue().toString();
                            String grade = " ";
                        }

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

                        }

                    });



                }
            });
        }
    }
}

【问题讨论】:

    标签: java android firebase firebase-realtime-database


    【解决方案1】:

    欢迎来到 StackOverflow。好吧,我对实时数据库也有类似的问题。这类数据库的问题在于它是有远见的快速和实时响应,这意味着这种类型的数据库是用于聊天应用程序的,通常是实时响应的。我的建议是将您的数据迁移到适用于此类应用程序的 Cloud Firestore 数据库,我遇到了类似的问题。

    以下是适用于 Android 的 Cloud Firestore 数据库的文档: https://firebase.google.com/docs/firestore

    实际上,这个函数是个问题:

    ref.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot snapshot) {
                            String name = snapshot.child("name").getValue().toString();
                            String email = snapshot.child("email").getValue().toString();
                            String grade = " ";
                        }
    
                        @Override
                        public void onCancelled(@NonNull DatabaseError error) {
    
                        }
    
                    });
    

    当数据库内的数据发生变化时,您正在检索数据,并且该事件不会发生。这就是我建议使用 Cloud Firestore 数据库的原因。

    我想这对你有帮助。 此致, 桑迪

    【讨论】:

    • 谢谢你的回答,我正在为我的大学制作这个应用程序,我应该使用实时数据库,我知道事件监听器有问题我试图检索用户数据并放置它在带有类数据的表中。我会尝试使用 Cloud Firestore。
    • 不客气,我的大学有一个项目,我使用了 RealTime DB,他们说没关系。希望对你有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-12
    • 1970-01-01
    • 1970-01-01
    • 2015-01-09
    相关资源
    最近更新 更多