This question already has answers here:
FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1
(2个答案)
2年前关闭。
我想通过在RecyclerView中使用FirebaseRecyclerAdapter而不通过使用FirebaseListAdapter来显示EmployeeDetails。但是活动什么也没显示
Firebase:
ViewEmployeeDetails.java
EmployeeViewHolder.java
activity_view_employee_details.xml
employee_list_activity.xml
我试图解决它,但是什么也没有发生。有人帮我。
然后,您需要在
由于适配器使用侦听器检查数据库更改,为了使其工作,我们需要首先开始侦听。当我们关闭应用程序时,我们需要停止监听。
我已经在我的videos之一中逐步说明了如何实现这一目标。
(2个答案)
2年前关闭。
我想通过在RecyclerView中使用FirebaseRecyclerAdapter而不通过使用FirebaseListAdapter来显示EmployeeDetails。但是活动什么也没显示
Firebase:
"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>
我试图解决它,但是什么也没有发生。有人帮我。
最佳答案
要解决此问题,请首先将适配器变量设置为全局变量:
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之一中逐步说明了如何实现这一目标。
08-04 02:23