当前位置: 首页 > 面试题库 >

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

濮阳霄
2023-03-14
问题内容

基本上,我想做的是使用,FirebaseRecyclerAdapterRecyclerView使用我的自定义设计填充CardView。较新版本的代码已更改,因此,我尝试实现它,但是没有用。这是我一年前编写的代码,可以很好地工作并填充我的代码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(我需要在此处进行哪些更改才能recyclerView用cardView
填充我的代码?)

@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的Model类:

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();
}

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

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

PS也请不要忘记使FBRA全局变量和FirebaseRecyclerAdapter<ServiceClass,ServiceViewHolder>从对象的声明中删除。



 类似资料:
  • 目前我有npm版本:5.5.1,但我想更新这个版本。当我试图更新时,它没有更新。我试图卸载并重新安装节点,但我仍然得到了相同的npm版本5.5.1。

  • Scala-2.11.8 文件夹位于“/home/hdadmin/”位置。 但是,当我再次检查scala-version时,它仍然显示了相同的2.9.2版本。它正在从 我需要知道是否有一个直接的方法来更新scala版本。

  • 在build.gradle文件中,我找到了这样的代码 如何从初始化位置更改值?1.

  • v2.0.9[2020-4-12] 修复(Fixed): 修复 MySQL 8.0 生成实体主键位置错误 e437d36 更新(Update): 连接池关闭连接, 如果连接已经断开, 异常将会被忽略 7aac80da 增强(Enhancement): v2.0.8[2020-1-18] 修复(Fixed): 修复 ws server 的 message response.finish 兼容 swo

  • 网防G01最新版本为:Linux版本:3.0.63.10,Windows版本:3.1.18.6,PC客户端版本:3.1.18。 更新记录: 2020年03月30日 2018年11月06日 2018年05月15日 2017年10月10日 2017年07月21日 2020年3月30日,更新说明 Linux版本:3.1.20.15 Windows版本:3.1.20.15 PC客户端版本:3.1.20.1