0

I REALLY have read all examples of the same problem but nothing helps I think mistake somewhere in another place. So the problem is that methods of recyclerview doesn't execute and list doesnt populate with data. Example I took from this place https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md

Also I checked this page https://github.com/happysingh23828/TheHappyChat/blob/master/app/src/main/java/happysingh/thehappychat/AllUsers.java Here is the code of the class

public class UsersActivity extends AppCompatActivity {

    //Layout
    private Toolbar mToolbar;
    private RecyclerView mUserList;

    //Firebase
    private DatabaseReference mUsersDatabase;

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

        //Layour
        mToolbar = (Toolbar) findViewById(R.id.users_toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setTitle("All Users");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        mUserList = (RecyclerView) findViewById(R.id.users_list);
//        mUserList.setHasFixedSize(true);
        mUserList.setLayoutManager(new LinearLayoutManager(this));

        //Firebase
        mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
    }

    @Override
    protected void onStart() {
        super.onStart();

        Log.v("=====> IN ON START", "in onstart");

        Query query = mUsersDatabase
                .limitToLast(20);

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

        //Pass model + viewholder
        //Pass model / layout of item / ViewHolder / reference to Databace place
        FirebaseRecyclerAdapter firebaseRecyclerAdapter = new FirebaseRecyclerAdapter
                <Users, UsersViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull UsersViewHolder holder, int position, @NonNull Users model) {

                String s = model.getName().toString();
                Log.v("=====> ", " " + s);
                holder.setName(model.getName());
                holder.setStatus(model.getStatus());
                holder.setImage(model.getImage());
            }

            @Override
            public UsersViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.user_item, parent, false);
                Log.v("=====> ", "Creating View Holder ");

                return new UsersViewHolder(view);
            }
        };

        mUserList.setAdapter(firebaseRecyclerAdapter);
        firebaseRecyclerAdapter.startListening();

    }

    public static class UsersViewHolder extends RecyclerView.ViewHolder {

        View view;
        Context c;

        public UsersViewHolder(View itemView) {
            super(itemView);
            view = itemView;
        }

        public void setName(String name) {

            TextView txtUserName = (TextView) view.findViewById(R.id.user_single_name);
            txtUserName.setText(name);


        }

        public void setStatus(String status) {
            TextView txtstatus = (TextView) view.findViewById(R.id.user_single_status);
            txtstatus.setText(status);
        }

        public void setImage(final String image) {

            final CircleImageView img = (CircleImageView) view.findViewById(R.id.user_single_img);;

            //  Picasso.with(c).load(image).into(img);

            Picasso.with(c).load(image).networkPolicy(NetworkPolicy.OFFLINE).into(img, new Callback() {
                @Override
                public void onSuccess() {
                    // Offline Download
                }

                @Override
                public void onError() {
                    Picasso.with(c).load(image).into(img);
                }
            });

        }

        public View getView() {
            return view;

        }
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Boris Ruzanov
  • 105
  • 1
  • 10

1 Answers1

1

To solve this, move all the code from your onStart() method inside onCreate() method, except these two lines of code:

super.onStart();
firebaseRecyclerAdapter.startListening();

And remove the static keyword from your UsersViewHolder class definition. It must be only:

public class UsersViewHolder extends RecyclerView.ViewHolder {}
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I tried it before. And without static. But how you initiate recyclerAdapter in this case? Now it is null pointer – Boris Ruzanov Feb 14 '18 at 15:50
  • When removing the static keyword it's not about how you initialize the `recyclerAdapter` is about how you initialize your `UsersViewHolder` class. And this must be made by using the **`new`** keyword, which means that you need to create every time a new instance of the class. Even if you are using that new keyword, you are **NOT** creating a new instance of the class, because you are using static keyword. So static keyword he has nothing to do there. Where are you getting a `NullPointerException`? – Alex Mamo Feb 14 '18 at 15:58
  • You understand me wrong))) never mind about static. Null pointer now on firebaseRecyclerAdapter.startListening() – Boris Ruzanov Feb 14 '18 at 16:03
  • As I said, all the code, except those two lines, must be moved to the `onCreate()` method. Remember to make `FirebaseRecyclerAdapter firebaseRecyclerAdapter` a global variable, to be seen also in your `onStart` method. All that code must be placed after `mUsersDatabase`. Does it work now? – Alex Mamo Feb 14 '18 at 16:06
  • Cool. Thanks! Everything works! But I am curious why in another examples it works in my it is not – Boris Ruzanov Feb 14 '18 at 16:14
  • You're welcome @BorisRuzanov ! Try to check also for that static keyword. Cheers! – Alex Mamo Feb 14 '18 at 16:15