首页 > 解决方案 > 为什么recyclerview数据只有在第二次点击按钮时才加载

问题描述

我正在开发基于位置的应用程序。我为此使用了firebase实时数据库。我正在匹配“职位发布”数据库和“应聘候选人”数据库​​中的公司名称,以便仅显示该特定公司的应聘候选人详细信息。一切正常,但问题是仅在第二次单击按钮时才加载 recyclerview 的数据。

public class AppliedCandidateActivity extends AppCompatActivity {
    Toolbar toolbarAppliedcandidate;
    RecyclerView rvAppliedCandidate;
    List<appliedData> ls;
    String compNameposted;

    AppCandidateAdapter adapter;

    DatabaseReference dbJobPost,dbAppliedCandidate;

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

        toolbarAppliedcandidate = findViewById(R.id.toolbarAppliedcandidate);
        rvAppliedCandidate = findViewById(R.id.rvAppliedCandidate);

        setSupportActionBar(toolbarAppliedcandidate);
        getSupportActionBar().setTitle("Applied Candidate");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        rvAppliedCandidate.setHasFixedSize(true);
        rvAppliedCandidate.setLayoutManager(new LinearLayoutManager(this));

        ls = new ArrayList<>();
        adapter = new AppCandidateAdapter(getApplicationContext(),ls);
        rvAppliedCandidate.setAdapter(adapter);

        getCompany();
        matchCompanyName();
}

    void getCompany()
    {
        //To retrieve company name
        dbJobPost = FirebaseDatabase.getInstance().getReference("Job Post").child(FirebaseAuth.getInstance().getCurrentUser().getUid());

        dbJobPost.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot ds : snapshot.getChildren())
                {
                    PostJobData postJobData = ds.getValue(PostJobData.class);
                    compNameposted = postJobData.getCompName().toString();

                    //Toast.makeText(getApplicationContext(),postJobData.getCompName(),Toast.LENGTH_LONG).show();

                }

            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });

    }

    void matchCompanyName()
    {
        //To retrieve data of applied candidate for particular company
        dbAppliedCandidate = FirebaseDatabase.getInstance().getReference("Applied Candidate");

        dbAppliedCandidate.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot snapshot) {
                for (DataSnapshot ds: snapshot.getChildren())
                {
                    for (DataSnapshot ds1 : ds.getChildren())
                    {
                        appliedData data = ds1.getValue(appliedData.class);
                        String compName = data.getCompName().toString();
                        //Toast.makeText(getApplicationContext(),compName,Toast.LENGTH_LONG).show();

                        if(compName.equals(compNameposted))
                        {

                            ls.add(data);
                        }
                        else if(ls.isEmpty()== true){
                            Toasty.info(AppliedCandidateActivity.this,"No One Applied Yet!!",Toast.LENGTH_LONG,true).show();
                        }

                    }

                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError error) {

            }
        });
    }

    public class AppCandidateAdapter extends RecyclerView.Adapter<AppCandidateAdapter.AppCandidateViewHolder>{
        List<appliedData> appliedDataList;
        Context context;

        public  AppCandidateAdapter(Context mcontext,List list){
            this.context = mcontext;
            this.appliedDataList = list;

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

        @Override
        public void onBindViewHolder(@NonNull AppCandidateViewHolder holder, int position) {
            appliedData data = appliedDataList.get(position);
            holder.tvCandidateName.setText(data.getName());
            holder.tvCandidateAppliedPost.setText(data.getPosition());
            holder.tvCandidateQual.setText(data.getQualification());
            holder.tvCandidateSkills.setText(data.getSkills());
        }

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

        class AppCandidateViewHolder extends RecyclerView.ViewHolder{
            TextView tvCandidateName,tvCandidateAppliedPost,tvCandidateQual,tvCandidateSkills;
            Button btnDeleteCandidate,btnSendMail;

            public AppCandidateViewHolder(@NonNull View itemView) {
                super(itemView);

                tvCandidateName = itemView.findViewById(R.id.tvCandidateName);
                tvCandidateAppliedPost = itemView.findViewById(R.id.tvCandidateAppliedPost);
                tvCandidateQual = itemView.findViewById(R.id.tvCandidateQual);
                tvCandidateSkills = itemView.findViewById(R.id.tvCandidateSkills);
                btnDeleteCandidate = itemView.findViewById(R.id.btnDeleteCandidate);
                btnSendMail = itemView.findViewById(R.id.btnSendMail);


            }
        }
    }


}

标签: androidfirebase-realtime-databaseandroid-recyclerview

解决方案


Assuming your onDataChange does get called, successfully reads the PostJobData data from the snapshot, and adds it to ls, you're not telling Android that the list has changed. Only once you notify the adapter of the change, will it rerender the view.

dbJobPost = FirebaseDatabase.getInstance().getReference("Job Post").child(FirebaseAuth.getInstance().getCurrentUser().getUid());

dbJobPost.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot snapshot) {
        for (DataSnapshot ds : snapshot.getChildren()) {
            PostJobData postJobData = ds.getValue(PostJobData.class);
            compNameposted = postJobData.getCompName().toString();
        }
        adapter.notifyDataSetChanged(); // notify the adapter
    }

    @Override
    public void onCancelled(@NonNull DatabaseError error) {
        throw error.toException(); // never ignore errors
    }
});

推荐阅读