【问题标题】:Android project issue [closed]Android项目问题[关闭]
【发布时间】:2020-02-02 18:23:34
【问题描述】:

我的 android 项目遇到了一些特殊问题。 特别是我有两个集合,用户和课程: 第一个理解具有两个字符串(电子邮件和 idCourse)的用户(教师)列表 第二个是课程列表,每个课程都与用户相关(idCourse 与用户相同),在每门课程中都有一个课程列表,我需要与特定用户一起访问。 我将其数据保存在对象管理员中以访问特定的课程集合,因为路径中有管理员 ID。

Courses path to Lessons Users collection

我试过了,但是当我在 addCompleteListener 方法中保存 id 时,数据会在外面丢失,实际上在 addSnapshotListener 中,admin.getCourseId() 的值为 null,因此我的 recyclerView 结果为空。 如果我尝试在第一种方法中复制第二种方法,只要我将 idCourse 保存在 Admin 中,该方法就会起作用,但是如果我在使用应用程序时单击课程,应用程序就会崩溃。 我知道这可能是我的一个具体问题,但我想听听您的意见。 我也在发布我的android项目。 Android Project on Google Drive 提前感谢您的帮助!

CLASS LezioniDocenteActivity:

public class LezioniDocenteActivity extends AppCompatActivity {

    private static final String TAG = "FireLog";
    private RecyclerView mMainList;
    private FirebaseFirestore mFirestore;
    private FirebaseAuth firebaseAuth;
    private LessonsListAdapter lessonsListAdapter;
    private List<Lesson> lessonList;
    private String course_id;

    //admin per memorizzare le info
    private final User admin = new User();

    @Override
    public void onBackPressed(){
        startActivity(new Intent(LezioniDocenteActivity.this, DocenteActivity.class));
    }

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

        lessonList = new ArrayList<>();
        lessonsListAdapter = new LessonsListAdapter(lessonList, getApplicationContext());

        mMainList = (RecyclerView)findViewById(R.id.lesson_list);
        mMainList.setHasFixedSize(true);
        mMainList.setLayoutManager(new LinearLayoutManager(this));
        mMainList.setAdapter(lessonsListAdapter);
        firebaseAuth = FirebaseAuth.getInstance();

        //utente in uso corrente
        final FirebaseUser user = firebaseAuth.getCurrentUser();
        admin.setEmail(user.getEmail());
        mFirestore = FirebaseFirestore.getInstance();

        mFirestore.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                boolean find = false;
                for (DocumentSnapshot doc : task.getResult()) {
                    String email = doc.getString("email");
                    if (email.equals(admin.getEmail())) {
                        //getting admin's courseID
                        admin.setCourseId(doc.getString("idCorso"));
                        break;
                    }
                }
            }
        });

        mFirestore.collection("Courses /" + admin.getCourseId() + "/Lessons").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
                if(e!=null){
                    Log.d(TAG, "Error :" + e.getMessage());
                    return;
                }
                for(DocumentChange doc : documentSnapshots.getDocumentChanges()){
                    if(doc.getType() == DocumentChange.Type.ADDED){
                        Lesson lesson = doc.getDocument().toObject(Lesson.class);
                        lessonList.add(lesson);
                        lessonsListAdapter.notifyDataSetChanged();
                    }
                }
            }
        });

    }
}

CLASS LessonListAdapter:

public class LessonsListAdapter extends RecyclerView.Adapter<LessonsListAdapter.ViewHolder> {

    public List<Lesson> lessonList;
    public Context context;
    final User admin = new User();

    public LessonsListAdapter(List<Lesson> lessonList, Context context){
        this.lessonList = lessonList;
        this.context = this.context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_lessons, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {

        final FirebaseFirestore firebaseFirestore = FirebaseFirestore.getInstance();
        final FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
        final FirebaseUser user = firebaseAuth.getCurrentUser();
        admin.setEmail(user.getEmail());

        holder.dayText.setText(lessonList.get(position).getLessonDate());
        holder.argText.setText(lessonList.get(position).getArgument());
        final String lessID = lessonList.get(position).lessonID;
        final int i = position;

        firebaseFirestore.collection("Users").get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        boolean find = false;
                        for(DocumentSnapshot doc : task.getResult()){
                            String email = doc.getString("email");

                            if(email.equals(admin.getEmail())) {
                                find = true;
                                //getting admin's courseID
                                admin.setCourseId(doc.getString("courseId"));
                                break;
                            }
                        }
            if (find){
                holder.delete.setVisibility(View.VISIBLE);
                holder.delete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(context, "OnClick funziona", Toast.LENGTH_SHORT).show();
                    }
                });
            } else holder.delete.setVisibility(View.GONE);
        }
    });
    }

    @Override
    public int getItemCount() {
        return lessonList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder{

        View mView;

        public TextView dayText;
        public TextView argText;
        public ImageView delete;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView;
            dayText = (TextView) mView.findViewById(R.id.day_text);
            argText = (TextView) mView.findViewById(R.id.arg_text);
            delete = (ImageView) mView.findViewById(R.id.ic_delete);
        }
    }
}

【问题讨论】:

  • 欢迎堆栈溢出:D 以后,请尽量使您的问题的标题更具体,Android project issue 并不能真正帮助任何人确定您的问题是什么,请记住:未来的用户或现有用户也可能遇到您的问题
  • 此外,当您遇到应用崩溃问题时,也请发布您的崩溃日志
  • 在这里看看这个:stackoverflow.com/help/how-to-askstackoverflow.com/tour 以获得一些帮助开始这里
  • 请写一个能真实描述您的问题的标题。

标签: java android firebase android-recyclerview


【解决方案1】:

以下代码将为您工作。但是您必须根据它进行更改。我没有使用 Android Studio。这是记事本代码。

mfirebaseFirestore.collection("Users").orderBy("email")
            .get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull Task<QuerySnapshot> task) {
            for (QueryDocumentSnapshot querySnapshot : task.getResult()){
                if (querySnapshot.get("email").equals(user.getEmail())){
                    mfirebaseFirestore.collection("Courses/"+ querySnapshot.get("idCorso")+"/Lessons")
                            .addSnapshotListener(new EventListener<QuerySnapshot>() {
                                @Override
                                public void onEvent(@Nullable QuerySnapshot documentSnapshots, @Nullable FirebaseFirestoreException e) {
                                    for(DocumentChange doc : documentSnapshots.getDocumentChanges()){
                                        if(doc.getType() == DocumentChange.Type.ADDED){
                                            Lesson lesson = doc.getDocument().toObject(Lesson.class);
                                            lessonList.add(lesson);
                                        }
                                        lessonsListAdapter = new LessonsListAdapter(lessonList, getApplicationContext());
                                        lessonsListAdapter.notifyDataSetChanged();
                                    }
                                }
                            });
                }
            }
        }
    });

代码说明:

正如大家所解释的,从上面的代码中,Firebase 调用是异步的(例如answer)。因此,正如您所说,您尝试检索的数据正在接收 null。

上面的代码将检查您的数据库中的电子邮件,然后检索该用户的详细信息。这意味着您将从中获得课程 ID。然后直接调用函数,通过传递课程id来检索课程列表。

【讨论】:

  • 应该 admin.setCourseId(doc.getString("idCorso"));每个周期都在 a 内吗?
  • 以上代码会直接提供单用户给你
  • 我的意思是,该文档与什么有关?因为没有声明
  • @GabrieleLoprieno 抱歉,我现在没有使用 ide,这是我的错,你可以试试吗?
  • 那个“abc”字符串是什么意思?我试图把 admin.getEmail() 和在这两种情况下它都不起作用,它什么也没显示.. 无论如何我也尝试使用 getString 而不是 get 但有相同的结果
猜你喜欢
  • 1970-01-01
  • 2013-01-09
  • 1970-01-01
  • 2021-01-26
  • 1970-01-01
  • 2017-07-31
  • 2019-08-19
  • 2016-05-20
  • 1970-01-01
相关资源
最近更新 更多