FirebaseRecyclerAdapter无法填充结果 [英] FirebaseRecyclerAdapter unable to populate result
问题描述
我想在RecyclerView中使用FirebaseRecyclerAdapter并不使用FirebaseListAdapter来显示EmployeeDetails。但活动没有显示任何内容
I want to display EmployeeDetails by using FirebaseRecyclerAdapter in RecyclerView and by not using FirebaseListAdapter. But the activity is displaying nothing
Firebase:
"EmployeeDetails" : {
"3dTxdRlBVUZ0jzH4U5MR4TOKZnD2" : {
"address" : "na",
"name" : "na",
"phoneNum" : "na",
"type" : "Employee"
},
"wEz6XRMQcAU9cQJLR82uVwVMrC83" : {
"address" : "na",
"name" : "na",
"phoneNum" : "na",
"type" : "Admin"
}
}
ViewEmployeeDetails.java
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
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
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
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>
我试着解决它但没有任何反应。有人帮助我。
I try to solve it but nothing happens. Someone helps me out.
推荐答案
要解决这个问题,首先要使你的适配器变量全局:
To solve this, first make your adapter variable global:
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();
}
}
因为适配器使用侦听器来检查数据库更改为了使它工作,我们需要先开始听。当我们关闭应用程序时,我们需要停止收听。
Because the adapter uses a listener to check for database changes, in order to make it work, we need to start listening first. And when we close the application, we need to stop listening.
我在 视频 ,如何实现这一点,一步一步。
I have explained in one of my videos, how to achieve this, step by step.
这篇关于FirebaseRecyclerAdapter无法填充结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!