如何在较新版本的Android 3.1和更高版本中实现Firebase Recycler Adapter? [英] How to implement Firebase Recycler Adapter in newer version of Android 3.1 and higher?

查看:85
本文介绍了如何在较新版本的Android 3.1和更高版本中实现Firebase Recycler Adapter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我想做的是使用FirebaseRecyclerAdapter并用我的自定义设计的CardView填充RecyclerView.较新版本的代码已更改,因此,我尝试实现它,但是没有用. 这是我一年前编写的代码,可以很好地工作并填充我的RecyclerView:

Basically, what I am trying to do is use a FirebaseRecyclerAdapter and populate the RecyclerView with my custom designed CardView. The code for newer versions has been changed and therefore, I tried implementing it but didn't work. This is the code I use to write a year ago, which worked fine and populated my 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?)

And now we have to use something like this, but the problem is this code is not populating my recyclerView (What changes do I need to make here to populate my recyclerView with my 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类:

And this is my Model class of getters and setters:

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 + '\'' +
            '}';
}
}

现在我需要做些什么更改?

Now what changes do I need to do?

这是我的整个Java文件:

Here is my entire java file:

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()方法:

In order to be able to display data from the Firebase realtime database you need to start listening for changes and for that you should add the following line of code in the onStart() method:

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

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

To stop listening foir changes you need add the following line of code in the onStop() method like this:

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

请从此 帖子中查看我的答案 我已经解释了为什么您应该删除监听器.

Please see my answer from this post where I have explained why you should remove the listener.

P.S.也请不要忘记将FBRA设置为全局变量,并将FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>从对象的声明中删除.

P.S. Please also don't forget to make the FBRA a global variable and remove FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder> from the declaration of the object.

这篇关于如何在较新版本的Android 3.1和更高版本中实现Firebase Recycler Adapter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆