如何在较新版本的Android 3.1及更高版本中实现Firebase回收器适配器



基本上,我要做的就是使用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);
    }
}

这是我的getters和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 + ''' +
            '}';
}
}

现在我需要做什么更改?

这是我的整个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();
    }
}

请参阅此 post 的答案,我解释了为什么您应该删除听众。

P.S。也请不要忘记使FBRA成为全局变量,然后从对象的声明中删除FirebaseRecyclerAdapter<ServiceClass, ServiceViewHolder>

这更多的是一个建议。如果要继续使用旧方法填充您的RecyClerview IE" PopulateViewHolder"方法而不是新的" OnBindViewHolder"方法,只需使用此;

implementation 'com.firebaseui:firebase-ui:1.0.1'

而不是升级的firebase-ui版本

相关内容

  • 没有找到相关文章

最新更新