Firebase回收器适配器不起作用

时间:2019-01-13 21:06:26

标签: android firebase firebase-realtime-database

我第一次尝试实现FirebaseRecyclerAdapter,但从未调用过它!我已经检查过Firebase Guide Here,但是不好。

还检查了报告“正在删除具有FixedSize()”的类似线程,为它们修复了类似的线程,但情况并非如此

Here's part of my database

这是我的适配器和型号代码:

 FirebaseRecyclerAdapter<Complaint, ComplaintHolder> TestAdapter;
DatabaseReference mRef;
FirebaseUser mUser;

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mUser = FirebaseAuth.getInstance().getCurrentUser();
    mRef = FirebaseDatabase.getInstance().getReference("AccountsComplaintBasdNode").child(mUser.getUid());


    Query query = mRef;


    FirebaseRecyclerOptions<Complaint> mOptions = new FirebaseRecyclerOptions.Builder<Complaint>()
            .setQuery(query, Complaint.class)
            .build();


    if (TestAdapter == null) {

        TestAdapter = new FirebaseRecyclerAdapter<Complaint, ComplaintHolder>(mOptions) {
            @NonNull
            @Override
            public ComplaintHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
                View v = LayoutInflater.from(getActivity()).inflate(R.layout.list_item, viewGroup, false);
                Toast.makeText(getActivity(), "Please get called", Toast.LENGTH_SHORT).show();
                return new ComplaintHolder(v);
            }


            @Override
            protected void onBindViewHolder(@NonNull ComplaintHolder holder, int position, @NonNull Complaint model) {

                holder.TitleTv.setText(model.getComplaintTitle());
                SimpleDateFormat smf = new SimpleDateFormat("yyyy/MM/dd");
                String dateString = smf.format(model.getDate());
                holder.DateTv.setText(dateString);

            }


///Model Object here

 public Complaint() {
}

private String ComplaintTitle;
private String details;
private Date mDate;
private String PhotoUrl;



public Complaint(String complaintTitle, String details, Date date, String photoUrl) {
    ComplaintTitle = complaintTitle;
    this.details = details;
    mDate = date;
    PhotoUrl = photoUrl;
}

public void setComplaintTitle(String complaintTitle) {
    ComplaintTitle = complaintTitle;
}

public void setDetails(String details) {
    this.details = details;
}

public void setDate(Date date) {
    this.mDate = date;
}

public void setPhotoUrl(String photoUrl) {
    PhotoUrl = photoUrl;
}

public String getComplaintTitle() {
    return ComplaintTitle;
}

public String getDetails() {
    return details;
}

public Date getDate() {
    return mDate;
}

public String getPhotoUrl() {
    return PhotoUrl;
}

1 个答案:

答案 0 :(得分:0)

FirebaseRecyclerAdapter使用事件侦听器监视Firebase查询的更改。要开始侦听数据,请调用startListening()方法。在调用startListening()之前,请确保已完成读取数据所需的所有身份验证,否则查询将失败。

因此,似乎您只需要在TestAdapter.startListening();方法中调用onStart(),并且不要忘记在TestAdapter.stopListening();中也要在onStop()中调用stopListening() }总是检查筹码null 像:if(TestAdapter != null)

有关更多信息和示例,请参见:https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md