为什么recyclerview数据只有在第二次点击按钮时才加载

时间:2020-12-23 13:37:26

标签: android firebase-realtime-database android-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);


            }
        }
    }


}

1 个答案:

答案 0 :(得分:0)

假设您的 import pandas as pd df = pd.DataFrame(list(zip(d.keys(),d.values())), columns=['key','value']) does 被调用,成功从快照读取 onDataChange 数据,并将其添加到 PostJobData,您不会告诉 Android该列表已更改。只有将更改通知适配器后,它才会重新渲染视图。

ls