Android 回收器返回时查看丢失的数据
我有一个NavigationDrawer活动,在那里我加载了一个包含Recyclerview的片段。回收器视图数据来自Firebase。第一次循环使用的观点是显示良好。但是,当我点击该项目时,转到另一个片段以查看详细信息,然后我点击后退按钮,回收视图显示为空 这是导航活动,这里有一个事务片段 这是EmployeeList片段Android 回收器返回时查看丢失的数据,android,firebase,android-recyclerview,Android,Firebase,Android Recyclerview,我有一个NavigationDrawer活动,在那里我加载了一个包含Recyclerview的片段。回收器视图数据来自Firebase。第一次循环使用的观点是显示良好。但是,当我点击该项目时,转到另一个片段以查看详细信息,然后我点击后退按钮,回收视图显示为空 这是导航活动,这里有一个事务片段 这是EmployeeList片段 public class EmployeeList extends Fragment { FirebaseDatabase firebaseDatabase;
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;
}
}
public类EmployeeList扩展片段{
FirebaseDatabase FirebaseDatabase;
数据库参考数据库参考;
字符串用户标识;
列出员工信息;
@BindView(R.id.employee\u list\u Recycler\u id)
RecyclerView employeeListRecyclerID;
视图;
@可空
@凌驾
创建视图时的公共视图(@NonNull LayoutInflater inflater、@Nullable ViewGroup container、@Nullable Bundle savedInstanceState){
如果(视图==null){
视图=充气机。充气(R.layout.employee_列表,空);
}
ButterKnife.bind(这个,视图);
返回视图;
}
@凌驾
已创建公用void onview(@NonNull视图,@Nullable Bundle savedInstanceState){
super.onViewCreated(视图,savedInstanceState);
firebaseDatabase=firebaseDatabase.getInstance();
databaseReference=firebaseDatabase.getReference();
employee_informations=new ArrayList();
//获取当前管理员用户ID
userID=getUserID();
//以列表形式获取员工信息
员工信息=getUserData();
GridLayoutManager GridLayoutManager=新的GridLayoutManager(getContext(),2);
employeeListRecyclerID.setLayoutManager(gridLayoutManager);
EmployeeList_Adapter Adapter Class_recycler=新的EmployeeList_Adapter(getContext(),员工信息);
employeeListRecyclerID.setAdapter(适配器类回收器);
adapterClass_recycler.setOnItemClickListener(新员工列表_适配器.ClickListener()){
@凌驾
公共空间单击(内部位置,视图v){
员工信息员工信息=新员工信息(
employee_informations.get(position).getUserID_employee(),
employee_informations.get(position).getUserID_company(),
员工信息.get(position).getEmail\u employee(),
employee_informations.get(position).getPassword_employee(),
employee_informations.get(position).getName_employee(),
员工信息。获取(职位)。获取加入日期(),
员工信息获取(职位).getImageLink(),
员工信息获取(职位).getDesignation(),
员工信息获取(职位).getPhone()
);
Fragment Fragment=新员工_profile_view_by_admin();
Bundle=新Bundle();
if(片段!=null){
bundle.putSerializable(“员工信息”,员工信息);
fragment.setArguments(bundle);
FragmentTransaction FragmentTransaction=getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.screen\u Area\u用于\u Admin,fragment);
fragmentTransaction.addToBackStack(“”);
fragmentTransaction.commit();
}
}
@凌驾
长点击(内部位置,视图v){
}
});
}
//以列表形式获取员工信息
私有列表getUserData(){
databaseReference.addValueEventListener(新的ValueEventListener(){
@凌驾
public void onDataChange(@NonNull DataSnapshot DataSnapshot){
for(DataSnapshot快照:DataSnapshot.child(“公司雇员”).child(userID.getChildren()){
Employee\u Information Employee\u Information=snapshot.getValue(Employee\u Information.class);
员工信息。添加(员工信息);
}
}
@凌驾
已取消的公共void(@NonNull DatabaseError DatabaseError){
}
});
返回员工信息;
}
//获取当前管理员用户ID
私有字符串getUserID(){
FirebaseAuth FirebaseAuth;
FirebaseUser FirebaseUser;
firebaseAuth=firebaseAuth.getInstance();
firebaseUser=firebaseAuth.getCurrentUser();
字符串userID=firebaseUser.getUid();
返回用户标识;
}
}
它将始终为空,因为作为方法的结果,您可以简单地返回列表。请检查重复项,了解您为什么会有这种行为,以及如何使用自定义回调解决此问题。它将始终为空,因为您可以简单地返回列表
,作为方法的结果。请检查重复项以了解为什么会出现这种行为,以及如何使用自定义回调解决此问题。
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;
}
}