使用 Firebase 刷新回收器视图后,Android 应用返回上一屏幕

时间:2021-03-09 12:04:55

标签: android firebase android-recyclerview

我正在开发一款培训应用。我想要实现的是删除项目并将其添加到从 Firebase 数据库中提取数据的回收站视图中。 问题是删除一些数据后,应用程序会回到上一个屏幕,我找不到任何导致它的错误消息。

这是我的活动代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    courseList = new ArrayList<>();

    ArrayList<Course> coursesToRemove = new ArrayList<>();
    removeCourseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (courseRecyclerAdapter.mCheckStates.size() == 0) {
                Utils.createAlertDialogWithOKButton(AdminManageCoursesActivity.this, "Alert", "Please select an item to remove");
            } else {
                for (int i = 0; i < courseList.size(); i++) {
                    if (courseRecyclerAdapter.mCheckStates.get(i) == true) {
                        coursesToRemove.add(courseList.get(i));
                    }
                }
                //get all the ticked checkboxes
                courseList.removeAll(coursesToRemove);
                //change the values in the DB

                for(Course course: coursesToRemove) {
                    Utils.courseReference.child(course.getCourseName()).removeValue();
                }

                //delete all user progress in db for deleted course
                for(User user : users) {
                    HashMap<String, Progress> removedProgressMap = user.getCourseProgress();
                    for(Course course: coursesToRemove) {
                        removedProgressMap.remove(course.getCourseName());
                    }
                    user.setCourseProgress(removedProgressMap);
                    Utils.userReference.child(Utils.removeSpecialCharactersFromEmail(user.getEmailAddress())).setValue(user);
                }
            }
        }
    });


    //get all the courses from the db
    Utils.courseReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            courseList = new ArrayList<>();
            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                courseList.add(dataSnapshot.getValue(Course.class));
            }
            //set the screen to the current courses initially
            courseRecyclerAdapter = new CourseRecyclerAdapter(courseList, true, null);
            coursesRecyclerView.setAdapter(courseRecyclerAdapter);
            filteredCourseList = courseList;

            //get all the user progresses from the db
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(AdminManageCoursesActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    users = new ArrayList<>();
    Utils.userReference.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {
            for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
                users.add(dataSnapshot.getValue(User.class));
            }
        }

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

        }
    });
}

}

Logcat 错误:

2021-03-09 16:24:46.798 1329-1498/? E/WindowManager: RemoteException occurs on reporting focusChanged, w=Window{eea87f3 u0 com.example.medicalEquipmentTraining/com.example.medicalEquipmentTraining.AdminManageCoursesActivity EXITING}
android.os.DeadObjectException
    at android.os.BinderProxy.transactNative(Native Method)
    at android.os.BinderProxy.transact(BinderProxy.java:575)
    at android.view.IWindow$Stub$Proxy.windowFocusChanged(IWindow.java:829)
    at com.android.server.wm.WindowState.reportFocusChangedSerialized(WindowState.java:3793)
    at com.android.server.wm.WindowManagerService$H.handleMessage(WindowManagerService.java:5433)
    at android.os.Handler.dispatchMessage(Handler.java:107)
    at android.os.Looper.loop(Looper.java:237)
    at android.os.HandlerThread.run(HandlerThread.java:67)
    at com.android.server.ServiceThread.run(ServiceThread.java:44)

另一个可能是(?)相关的

2021-03-09 17:19:56.989 1329-1368/? E/WindowManager: win=Window{e20d4c9 u0 com.example.medicalEquipmentTraining/com.example.medicalEquipmentTraining.AdminManageCoursesActivity} destroySurfaces: appStopped=true win.mWindowRemovalAllowed=false win.mRemoveOnExit=false win.mViewVisibility=8 caller=com.android.server.wm.AppWindowToken.destroySurfaces:1248 com.android.server.wm.AppWindowToken.destroySurfaces:1229 com.android.server.wm.AppWindowToken.notifyAppStopped:1284 com.android.server.wm.ActivityRecord.activityStoppedLocked:2776 com.android.server.wm.ActivityTaskManagerService.activityStopped:2512 android.app.IActivityTaskManager$Stub.onTransact:2280 android.os.Binder.execTransactInternal:1056

编辑:

进一步调试后看起来问题是由这段代码引起的:

for(User user : users) {
                HashMap<String, Progress> removedProgressMap = user.getCourseProgress();
                for(Course course: coursesToRemove) {
                    removedProgressMap.remove(course.getCourseName());
                }
                user.setCourseProgress(removedProgressMap);
                Utils.userReference.child(Utils.removeSpecialCharactersFromEmail(user.getEmailAddress())).setValue(user);
            }

我仍然没有在 logcat 中收到任何与此相关的错误消息,并且数据库交互已成功完成。它只是回到上一个活动......不知道如何解决这个问题,我尝试在一个查询中一起获取数据,并尝试在 Firebase 中设置值时设置一个 CompletionListener 。如果我在没有第二次数据库更新的情况下尝试过,它工作正常。 所以我认为我的问题是尝试更新两个单独的节点?

1 个答案:

答案 0 :(得分:0)

所以看起来整个应用程序都重新启动了,因为我在第二个查询中更新了当前登录的用户。一旦我避免这样做,它就可以正常工作。