gpt4 book ai didi

java - 如何在较新版本的 Android 3.1 及更高版本中实现 Firebase Recycler Adapter?

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:55:31 25 4
gpt4 key购买 nike

基本上,我想做的是使用 FirebaseRecyclerAdapter 并使用我自定义设计的 CardView 填充 RecyclerView。新版本的代码已经更改,因此,我尝试实现它但没有成功。这是我一年前编写的代码,它运行良好并填充了我的 RecyclerView:

FirebaseRecyclerAdapter<DataClass,DataViewHolder> FBRA= new FirebaseRecyclerAdapter<DataClass, DataViewHolder>(
DataClass,
R.layout.myCardView,
DataViewHolder.class,
databaseReference
) {
@Override
protected void populateViewHolder(DataViewHolder viewHolder, DataClass model, int position) {
viewHolder.setTitle(model.gettitle());
viewHolder.setDate(model.getDate());
}
};
myRecyclerView.setAdapter(FBRA);

现在我们必须使用这样的东西,但问题是此代码未填充我的 recyclerView(我需要在此处进行哪些更改才能使用我的 cardView 填充我的 recyclerView?)

@Override
protected void onStart() {
super.onStart();

Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Official_Services");

FirebaseRecyclerOptions<ServiceClass> options = new FirebaseRecyclerOptions.Builder<ServiceClass>()
.setQuery(query, ServiceClass.class)
.build();

FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder> FBRA = new FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>(options) {

@NonNull
@Override
public ServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(HomeActivity.this).inflate(R.layout.service_card, parent, false);

return new ServiceViewHolder(view);

}

@Override
protected void onBindViewHolder(@NonNull ServiceViewHolder holder, int position, @NonNull ServiceClass model) {

holder.setServiceName(model.getServiceName());

holder.setServiceCaption(model.getServiceCaption());

}

};

mServiceList.setAdapter(FBRA);

}

这是我的 ViewHolder 类:

public static class ServiceViewHolder extends RecyclerView.ViewHolder {

public ServiceViewHolder(View itemView) {

super(itemView);

View mView = itemView;

}

public void setServiceName(String serviceName) {

TextView sName = itemView.findViewById(R.id.serviceName);

sName.setText(serviceName);

}

public void setServiceCaption(String serviceCaption) {

TextView sCaption = itemView.findViewById(R.id.serviceCap);

sCaption.setText(serviceCaption);
}

}

这是我的 getter 和 setter 模型类:

public class ServiceClass {

private String serviceName;
private String serviceCode;
private String serviceCaption;
private String serviceIconUrl;

public ServiceClass() {
}

public ServiceClass(String serviceName, String serviceCode, String serviceCaption, String serviceIconUrl) {
this.serviceName = serviceName;
this.serviceCode = serviceCode;
this.serviceCaption = serviceCaption;
this.serviceIconUrl = serviceIconUrl;
}

public String getServiceName() {
return serviceName;
}

public String getServiceCode() {
return serviceCode;
}

public String getServiceCaption() {
return serviceCaption;
}

public String getServiceIconUrl() {
return serviceIconUrl;
}

public void setServiceName(String serviceName) {
this.serviceName = serviceName;
}

public void setServiceCode(String serviceCode) {
this.serviceCode = serviceCode;
}

public void setServiceCaption(String serviceCaption) {
this.serviceCaption = serviceCaption;
}

public void setServiceIconUrl(String serviceIconUrl) {
this.serviceIconUrl = serviceIconUrl;
}

@Override
public String toString() {
return "ServiceClass{" +
"serviceName='" + serviceName + '\'' +
", serviceCode='" + serviceCode + '\'' +
", serviceCaption='" + serviceCaption + '\'' +
", serviceIconUrl='" + serviceIconUrl + '\'' +
'}';
}
}

现在我需要做哪些改变?

这是我的整个 java 文件:

public class HomeActivity extends AppCompatActivity {

private RecyclerView mServiceList;

private FirebaseDatabase mDatabase;

private DatabaseReference myRef;

FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder> FBRA;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);

BottomNavigationViewEx bottomNavigationViewEx = findViewById(R.id.navViewBar);

bottomNavigationViewEx.enableAnimation(false);

bottomNavigationViewEx.enableShiftingMode(false);

bottomNavigationViewEx.setTextVisibility(false);

Calligrapher calligrapher = new Calligrapher(this);

calligrapher.setFont(this, "Helvetica.ttf", true);

mServiceList = findViewById(R.id.serviceRV);

mServiceList.setHasFixedSize(true);

mServiceList.setLayoutManager(new LinearLayoutManager(this));

mDatabase = FirebaseDatabase.getInstance();

myRef = mDatabase.getReference().child("Official_Services");

}

@Override
protected void onStart() {
super.onStart();

FBRA.startListening();

Query query = myRef;

FirebaseRecyclerOptions<ServiceClass> options = new FirebaseRecyclerOptions.Builder<ServiceClass>()
.setQuery(query, ServiceClass.class)
.build();

FBRA = new FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>(options) {

@NonNull
@Override
public ServiceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {

View view = LayoutInflater.from(HomeActivity.this).inflate(R.layout.service_card, parent, false);

return new ServiceViewHolder(view);

}

@Override
protected void onBindViewHolder(@NonNull ServiceViewHolder holder, int position, @NonNull ServiceClass model) {

holder.setServiceName(model.getServiceName());

holder.setServiceCaption(model.getServiceCaption());

}

};

mServiceList.setAdapter(FBRA);



}



public static class ServiceViewHolder extends RecyclerView.ViewHolder {

public ServiceViewHolder(View itemView) {

super(itemView);

View mView = itemView;

}

public void setServiceName(String serviceName) {

TextView sName = itemView.findViewById(R.id.serviceName);

sName.setText(serviceName);

}

public void setServiceCaption(String serviceCaption) {

TextView sCaption = itemView.findViewById(R.id.serviceCap);

sCaption.setText(serviceCaption);
}

}

}

最佳答案

为了能够显示来自 Firebase 实时数据库的数据,您需要开始监听更改,为此您应该在 onStart() 中添加以下代码行方法:

@Override
protected void onStart() {
super.onStart();
FBRA.startListening();
}

要停止监听更改,您需要在 onStop() 中添加以下代码行像这样的方法:

@Override
protected void onStop() {
super.onStop();
if(FBRA != null) {
FBRA.stopListening();
}
}

请看我在这个 post中的回答 我在其中解释了为什么您应该删除监听器。

附言也请不要忘记制作 FBRA一个全局变量并删除 FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>来自对象的声明。

关于java - 如何在较新版本的 Android 3.1 及更高版本中实现 Firebase Recycler Adapter?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55025409/

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