0

I am new to Android Studio..so please help me out. I am working on app which shows the list of student in recycler view,I am retriving the list from Fire base Database but i'm getting the error at UserAdapter constructor class (I don't know what to provide in Super()).

I have used standard RecyclerAdapter it work the way I wanted but I couldn't get the UID of student when person clicks on it ....so I switched to FirebaseRecyclerAdapter ...here I can retrive the UID easily ...but getting error at constructor of UserAdapter.

I think I have to pass the FirebaseRecyclerOptions in my case its "options" but its show error "Cannot reference 'UserAdapter.options' before supertype constructor has been called" and I don't know how to make the options Static.

I tried to make it static like this

public static FirebaseRecyclerOptions options;

in classmates class and accessed it in super() as super(classmates.this.options);i know its wrong method and then I declared in UserAdapter class but didnt work.

sorry for the long post.please help me out.

Here's the code:

  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);
                }
            }

        }
    }
Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Seeon
  • 81
  • 1
  • 1
  • 11
  • What is the exact error that you get? – Alex Mamo Aug 20 '18 at 10:02
  • I am getting the line error at super() in constructor as **FirebaseRecyclerAdapter (FirebaseRecyclerOptions) in FirebaseRecyclerAdapter cannot be applied** and if I pass options as an value I get the error as mention above **Cannot reference 'UserAdapter.options' before supertype constructor has been called** – Seeon Aug 20 '18 at 10:35
  • **[This](https://stackoverflow.com/questions/49383687/how-can-i-retrieve-data-from-firebase-to-my-adapter/49384849)** is a recommended way in which you can retrieve data from a Firebase Realtime database and display it in a `RecyclerView` using `FirebaseRecyclerAdapter`. – Alex Mamo Aug 20 '18 at 11:18
  • I even tried to use the UserAdapter class in other file (.java) but getting the same error .... And tried to write in onStart() to avoid those error ....but couldn’t find the way to send my list from classmates . – Seeon Aug 20 '18 at 11:24

0 Answers0