我想在RecyclerView中使用FirebaseRecyclerAdapter并不使用FirebaseListAdapter来显示EmployeeDetails。但活动没有显示任何内容
火力地堡:
"EmployeeDetails" : {
"3dTxdRlBVUZ0jzH4U5MR4TOKZnD2" : {
"address" : "na",
"name" : "na",
"phoneNum" : "na",
"type" : "Employee"
},
"wEz6XRMQcAU9cQJLR82uVwVMrC83" : {
"address" : "na",
"name" : "na",
"phoneNum" : "na",
"type" : "Admin"
}
}
ViewEmployeeDetails.java
public class ViewEmployeeDetails extends AppCompatActivity {
RecyclerView recyclerView;
RecyclerView.LayoutManager layoutManager;
DatabaseReference employee;
FirebaseRecyclerAdapter<UserInformation,EmployeeViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_employee_details);
recyclerView=(RecyclerView)findViewById(R.id.listEmployee);
recyclerView.setHasFixedSize(true);
layoutManager=new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
loadEmployee();
}
private void loadEmployee() {
employee=
FirebaseDatabase.getInstance().getReference("EmployeeDetails");
adapter=new FirebaseRecyclerAdapter<UserInformation, EmployeeViewHolder>
(
UserInformation.class,
R.layout.employee_list_activity,
EmployeeViewHolder.class,
employee
) {
@Override
protected void populateViewHolder(EmployeeViewHolder viewHolder,
UserInformation model, int position) {
viewHolder.txtEmployeeName.setText(model.getName());
viewHolder.txtEmployeeType.setText(model.getType());
viewHolder.txtEmployeeAddress.setText(model.getAddress());
viewHolder.txtEmployeePhone.setText(model.getPhoneNum());
}
};
adapter.notifyDataSetChanged();
recyclerView.setAdapter(adapter);
}
}
EmployeeViewHolder.java
package com.example.intel.employeesite.Other.ViewHolders;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import com.example.intel.employeesite.R;
public class EmployeeViewHolder extends RecyclerView.ViewHolder {
public TextView
txtEmployeeName,txtEmployeeAddress,txtEmployeePhone,txtEmployeeType;
public EmployeeViewHolder(View itemView) {
super(itemView);
txtEmployeeName=(TextView) itemView.findViewById(R.id.txtEmployeeName);
txtEmployeeAddress=(TextView)
itemView.findViewById(R.id.txtEmployeeAddress);
txtEmployeePhone=(TextView)
itemView.findViewById(R.id.txtEmployeePhone);
txtEmployeeType=(TextView) itemView.findViewById(R.id.txtEmployeeType);
}
}
activity_view_employee_details.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.intel.employeesite.Admin.ViewEmployeeDetails">
<android.support.v7.widget.RecyclerView
android:id="@+id/listEmployee"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
employee_list_activity.xml
<CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
app:cardElevation="10dp"
android:id="@+id/empinfo"
android:layout_margin="5dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/txtEmployeeName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Employee Name"
android:textSize="20sp"
android:background="#4f0e0d0e"
android:textColor="#fff"/>
<TextView
android:id="@+id/txtEmployeeAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Employee Address"
android:textSize="20sp"
android:background="#4f0e0d0e"
android:textColor="#fff"/>
<TextView
android:id="@+id/txtEmployeePhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Employee Phone"
android:textSize="20sp"
android:background="#4f0e0d0e"
android:textColor="#fff"/>
<TextView
android:id="@+id/txtEmployeeType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Employee Type"
android:textSize="20sp"
android:background="#4f0e0d0e"
android:textColor="#fff"/>
</LinearLayout>
</CardView>
我试着解决它但没有任何反应。有人帮助我。
答案 0 :(得分:0)
要解决此问题,首先要使适配器变量为全局变量:
private FirebaseRecyclerAdapter<UserInformation, EmployeeViewHolder> adapter;
然后,您需要在onStart()
和onStop()
方法中添加以下代码行:
@Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
if(adapter != null) {
adapter.stopListening();
}
}
因为适配器使用侦听器来检查数据库更改,所以为了使其工作,我们需要先开始侦听。当我们关闭应用程序时,我们需要停止收听。
我已经在我的 videos 中解释了如何逐步实现这一目标。