0

我正在开发一个 android 应用程序,其中 Firebase 实时数据库的详细信息将显示在 Recycler View 中。我想让它像可扩展的recyclerview。但是当我尝试使用我的适配器时出现错误。

变量“适配器”可能尚未初始化

    @Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerOptions options =
            new FirebaseRecyclerOptions.Builder<Employee>()
                    .setQuery(mAttendanceDatabase, Employee.class)
                    .build();

    final FirebaseRecyclerAdapter<Employee, EmployeeViewHolder> adapter = new FirebaseRecyclerAdapter<Employee, EmployeeViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final EmployeeViewHolder holder, final int position, @NonNull Employee model) {
                    String employee_Id = getRef(position).getKey();
                    assert employee_Id != null;
                    mAttendanceDatabase.child(employee_Id).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            String employee_Name = dataSnapshot.child("employeeName").getValue().toString();
                            holder.employeeName.setText(employee_Name);
                            holder.btn_viewMore.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    adapter.getItem(position);        //I'm getting error here

                                }
                            });

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Toast.makeText(getContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @NonNull
                @Override
                public EmployeeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_view_layout, parent, false);
                    EmployeeViewHolder viewHolder = new EmployeeViewHolder(view);
                    return viewHolder;
                }
            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();
}


    public static class EmployeeViewHolder extends RecyclerView.ViewHolder{
    TextView employeeName;
    ImageButton btn_viewMore;
    public EmployeeViewHolder(@NonNull View itemView) {
        super(itemView);
        employeeName = itemView.findViewById(R.id.employeeName_evl);
        btn_viewMore = itemView.findViewById(R.id.viewMoreBtn);
    }
}

我找不到任何合适的解决方案。帮我解决这个问题。

4

1 回答 1

0

尝试将适配器放在this变量中:

@Override
public void onStart() {
    super.onStart();
    FirebaseRecyclerOptions options =
            new FirebaseRecyclerOptions.Builder<Employee>()
                    .setQuery(mAttendanceDatabase, Employee.class)
                    .build();

    final FirebaseRecyclerAdapter<Employee, EmployeeViewHolder> adapter = new FirebaseRecyclerAdapter<Employee, EmployeeViewHolder>(options) {
                @Override
                protected void onBindViewHolder(@NonNull final EmployeeViewHolder holder, final int position, @NonNull Employee model) {
                    String employee_Id = getRef(position).getKey();
                    assert employee_Id != null;
                    final FirebaseRecyclerAdapter<Employee, EmployeeViewHolder> thisAdapter = this;
                    mAttendanceDatabase.child(employee_Id).addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            String employee_Name = dataSnapshot.child("employeeName").getValue().toString();
                            holder.employeeName.setText(employee_Name);
                            holder.btn_viewMore.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {

                                    thisAdapter.getItem(position);        //I'm getting error here

                                }
                            });

                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                            Toast.makeText(getContext(), "Some error occurred", Toast.LENGTH_SHORT).show();
                        }
                    });
                }

                @NonNull
                @Override
                public EmployeeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.employee_view_layout, parent, false);
                    EmployeeViewHolder viewHolder = new EmployeeViewHolder(view);
                    return viewHolder;
                }
            };

    recyclerView.setAdapter(adapter);
    adapter.startListening();
}

编辑

为了支持我的案例,我做了一个最小的例子:

作品

作品不

于 2020-07-02T07:44:08.273 回答