我退出并重新进入活动后或按返回按钮时,Recyclerview会加载

时间:2019-03-11 14:01:29

标签: android android-recyclerview

我的应用程序中有一个recyclerview,但是它仅在我退出并重新进入活动后才加载数据。我已经尝试了下面的线程,但无法让我的工作。

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

我尝试过

adapter.notifyDataSetChanged()

仍然无法正常工作

主要活动

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

是否缺少任何内容? 有任何建议或想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用:在{> $ a='..--' $ [[ $a =~ ${a//?/(.)} ]] $ b=${BASH_REMATCH[*]:1} $ echo "${b// /\{,\" \"\}}" .\{," "\}.\{," "\}-\{," "\}- 之前使用adapter.startListening();,然后从recent_patients_list.setAdapter(adapter);中删除adapter.notifyDataSetChanged();

还要像这样修改您的onCreate

onStart()

您在@Override protected void onStart() { super.onStart(); } 中什么也不做!