回收者返回时查看丢失的数据

时间:2019-03-12 11:41:12

标签: android firebase android-recyclerview

我有一个NavigationDrawer活动,在那里加载了一个包含Recyclerview的Fragment。回收者视图数据来自Firebase。第一次显示回收视图。但是,当我单击该项目时,请转到另一个片段以查看详细信息,然后单击“后退”按钮,回收的视图将显示为空。

这是导航活动,其中有我交易的片段

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main__admin);
    Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    ------------------------
 ------------------------
    -------
    // default fragment set up
    Fragment fragment1 = new EmployeeList();
    if (fragment1!=null){
        Toast.makeText(this, "+call the", Toast.LENGTH_SHORT).show();
        FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.replace(R.id.screen_Area_For_Admin, fragment1);
        fragmentTransaction.commit();
    }

}

这是EmployeeList片段

public class EmployeeList extends Fragment {

    FirebaseDatabase firebaseDatabase;
    DatabaseReference databaseReference;
    String userID;

    List<Employee_Information> employee_informations;

    @BindView(R.id.employee_list_Recycler_ID)
    RecyclerView employeeListRecyclerID;

    View view;
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view==null){
            view = inflater.inflate(R.layout.employee_list, null);
        }
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        firebaseDatabase = FirebaseDatabase.getInstance();
        databaseReference = firebaseDatabase.getReference();
        employee_informations = new ArrayList<>();
//get Current Admin User ID
        userID = getUserID();
//get Employee Information as a list
        employee_informations = getUserData();

        GridLayoutManager gridLayoutManager = new GridLayoutManager(getContext(), 2);

        employeeListRecyclerID.setLayoutManager(gridLayoutManager);

        EmployeeList_Adapter adapterClass_recycler = new EmployeeList_Adapter(getContext(), employee_informations);

        employeeListRecyclerID.setAdapter(adapterClass_recycler);

        adapterClass_recycler.setOnItemClickListener(new EmployeeList_Adapter.ClickListener() {
            @Override
            public void onItemClick(int position, View v) {

                Employee_Information employee_information = new Employee_Information(
                        employee_informations.get(position).getUserID_Employee(),
                        employee_informations.get(position).getUserID_company(),
                        employee_informations.get(position).getEmail_Employee(),
                        employee_informations.get(position).getPassword_Employee(),
                        employee_informations.get(position).getName_Employee(),
                        employee_informations.get(position).getJoin_Date(),
                        employee_informations.get(position).getImageLink(),
                        employee_informations.get(position).getDesignation(),
                        employee_informations.get(position).getPhone()
                );

                Fragment fragment = new Employee_profile_view_by_admin();
                Bundle bundle = new Bundle();

                if (fragment != null) {
                    bundle.putSerializable("employee_information", employee_information);
                    fragment.setArguments(bundle);

                    FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
                    fragmentTransaction.replace(R.id.screen_Area_For_Admin, fragment);
                    fragmentTransaction.addToBackStack("");
                    fragmentTransaction.commit();
                }
            }

            @Override
            public void onItemLongClick(int position, View v) {

            }
        });


    }

    //get Employee Information as a list
    private List<Employee_Information> getUserData() {
        databaseReference.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.child("Company_Employee").child(userID).getChildren()) {

                    Employee_Information employee_information = snapshot.getValue(Employee_Information.class);
                    employee_informations.add(employee_information);
                }
            }

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

            }
        });

        return employee_informations;
    }

    //get Current Admin User ID
    private String getUserID() {

        FirebaseAuth firebaseAuth;
        FirebaseUser firebaseUser;
        firebaseAuth = FirebaseAuth.getInstance();
        firebaseUser = firebaseAuth.getCurrentUser();
        String userID = firebaseUser.getUid();
        return userID;
    }

}

0 个答案:

没有答案