FirebaseRecyclerAdapter(将列表发送到UserAdapter类)

时间:2018-08-20 09:32:59

标签: java firebase firebase-realtime-database firebase-authentication

我是Android Studio的新手。.所以请帮帮我。 我正在开发在回收站视图中显示学生列表的应用程序,我正在从Fire base数据库中检索该列表,但在 UserAdapter 构造函数类中出现错误(我没有知道要在Super())中提供什么。

我使用了标准的 RecyclerAdapter ,但是我无法获得学生的UID ....所以我切换到 FirebaseRecyclerAdapter strong> ...在这里我可以轻松检索 UID ...但在UserAdapter的构造函数中出现错误。

我认为我必须通过FirebaseRecyclerOptions它的“选项”,但它显示错误 “ 在超类型构造函数被调用之前无法引用'UserAdapter.options'”,而且我不知道如何将选项设置为静态。

我试图像这样使它静止

  

公共静态FirebaseRecyclerOptions选项;

同学类中并以super()的身份在super(classmates.this.options);中访问它,我知道它的方法错误,然后我在 UserAdapter类中声明了,但是没有用

很长的帖子很抱歉。请帮帮我。

代码如下:

  public class classmates extends AppCompatActivity {

        Toolbar toolbar;
        private RecyclerView ClassmateList;

        private DatabaseReference rDatabaseRef;
        List<Users> list = new ArrayList<>();
        FirebaseRecyclerAdapter adapter;


        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_classmates);

            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Classmates");

            rDatabaseRef = FirebaseDatabase.getInstance().getReference().child("user");

            ClassmateList = (RecyclerView) findViewById(R.id.recyclerView);
            ClassmateList.setHasFixedSize(true);
            ClassmateList.setLayoutManager(new LinearLayoutManager(getApplicationContext()));

            rDatabaseRef.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot Snapshot) {

                    for (DataSnapshot dataSnapshot : Snapshot.getChildren()) {
                        Users studentDetails = dataSnapshot.getValue(Users.class);
                        list.add(studentDetails);
                    } 
                     adapter = new UserAdapter(list, classmates.this);
                     ClassmateList.setAdapter(adapter);
                }

                @Override
                public void onCancelled(@NonNull DatabaseError databaseError) {
                }
            });
        }

        public class UserAdapter extends FirebaseRecyclerAdapter<Users, UserAdapter.UserViewHolder> {

            Context context1;
            List<Users> InfoList;

            FirebaseRecyclerOptions<Users> options =
                    new FirebaseRecyclerOptions.Builder<Users>()
                            .setQuery(rDatabaseRef, Users.class)
                            .build();

            public UserAdapter(List<Users> list, classmates classmates) {
                super(); *<---ERROR ,DONT KNOW WHAT TO PROVIDE* 
                context1=classmates;
                InfoList=list;
            }

            @Override
            protected void onBindViewHolder(@NonNull UserViewHolder holder, int position, @NonNull Users model) {
                final Users users = InfoList.get(position);
                holder.setName(users.getName());
                holder.setStatus(users.getStatus());
                holder.setThumb_img(users.getThumb_img());

                String userid=getRef(position).getKey();
            }

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

            @Override
            public int getItemCount() {
                return InfoList.size();
            }


            public class UserViewHolder extends RecyclerView.ViewHolder {

                View rview;

                public UserViewHolder(View itemView) {
                    super(itemView);
                    rview = itemView;
                }

                public void setName(String name) {

                    TextView singleusername = (TextView) rview.findViewById(R.id.single_displayname);
                    singleusername.setText(name);
                }

                public void setStatus(String status) {

                    TextView singleuserstatus = (TextView) rview.findViewById(R.id.single_status);
                    singleuserstatus.setText(status);
                }

                public void setThumb_img(String thumb_image) {
                    CircleImageView user_img = (CircleImageView) rview.findViewById(R.id.single_thumb_image);
                    Picasso.get().load(thumb_image).placeholder(R.drawable.default_img).into(user_img);
                }
            }

        }
    }

0 个答案:

没有答案