gpt4 book ai didi

java - FirebaseRecyclerAdapter 无法填充结果

转载 作者:太空宇宙 更新时间:2023-11-04 10:41:37 25 4
gpt4 key购买 nike

我想通过在 RecyclerView 中使用 FirebaseRecyclerAdapter 而不是使用 FirebaseListAdapter 来显示 EmployeeDetails。但该 Activity 没有显示任何内容

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 中进行了解释。 ,如何一步步实现这一目标。

关于java - FirebaseRecyclerAdapter 无法填充结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48908750/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com