0

I have a recyclerview in my app, but it only loads the data after i exit and re-enter the activity. I have tried the threads below,but couldn't get mine to work.

RecyclerView not populating the first time I enter the activity but showing after i exit and re-enter the activity

RecyclerView doesn't load items on first start of activity

I tried,

adapter.notifyDataSetChanged()

and still didn't work

Main Activity

public class Review1 extends AppCompatActivity {
    private RecyclerView recent_patients_list;
    private FirebaseAuth mAuth;
    public static final String REVIEW_ID ="review_id";
    FirebaseRecyclerAdapter<Patients, PatientsViewHolder> adapter;


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

        ///Firebase Authentication
        mAuth = FirebaseAuth.getInstance();
        FirebaseUser mUser = mAuth.getCurrentUser();
        String uId = mUser.getUid();
        Query query = FirebaseDatabase.getInstance().getReference().child("Patients").child(uId).limitToLast(50);
        query.keepSynced(true);

        recent_patients_list = findViewById(R.id.recycler);
        recent_patients_list.setHasFixedSize(true);

        ///FirebaseUI RecyclerView
        FirebaseRecyclerOptions<Patients> options =
                new FirebaseRecyclerOptions.Builder<Patients>()
                        .setQuery(query, Patients.class)
                        .build();


        adapter = new FirebaseRecyclerAdapter<Patients, PatientsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(@NonNull PatientsViewHolder holder, final int position, @NonNull Patients model) {
                holder.setTitle(model.getName());
                holder.setNote(model.getResidence());
                holder.setNote1(model.getOccupation());
                holder.setDate(model.getDate());

                //To get the id and pass to Review2
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String user_id = getRef(position).getKey(); //// get key to use in Review2.java
                        Intent intent = new Intent(getApplicationContext(),Review2.class);
                        intent.putExtra(REVIEW_ID, user_id);/// pass key into intent
                        startActivity(intent);
                    }
                });



            }

            @NonNull
            @Override
            public PatientsViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

                View view = LayoutInflater.from(viewGroup.getContext())
                        .inflate(R.layout.patient_listview,viewGroup,false);
                return new PatientsViewHolder(view);
            }
        };


        recent_patients_list.setLayoutManager(new LinearLayoutManager(this));
        recent_patients_list.setAdapter(adapter);///setting adapter for the recyclerView
        adapter.startListening();
        adapter.notifyDataSetChanged();
    }





    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
        adapter.notifyDataSetChanged();

    }

    ////ViewHolder for firebase recycler view

    private class PatientsViewHolder extends RecyclerView.ViewHolder {
        View mView;
        public PatientsViewHolder(@NonNull View itemView) {
            super(itemView);
            mView = itemView;

        }
        public void  setTitle(String title){
            TextView mTitle = mView.findViewById(R.id.Title);
            mTitle.setText("Name:"  + title);

        }

        public void  setNote(String note){
            TextView mNote = mView.findViewById(R.id.other);
            mNote.setText("Location:" + note);
        }
        void  setNote1(String note1){
            TextView mNote1 = mView.findViewById(R.id.other2);
            mNote1.setText("Occupation" +note1);
        }
        public void  setDate(String date){
            TextView mDate = mView.findViewById(R.id.date);
            mDate.setText(date);
        }
        }
}

Is there anything am missing? Any suggestions or ideas?

PhxIT
  • 29
  • 6

1 Answers1

1

Try to use: adapter.startListening(); before -> recent_patients_list.setAdapter(adapter); and remove adapter.notifyDataSetChanged(); from onCreate

And also modify your onStart() like:

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

You are doing nothing in onPause !

Murat Güç
  • 377
  • 4
  • 9