简体   繁体   English

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

[英]Why does the recyclerview data loads only when the button is clicked the second time

I am developing an app based on placement.我正在开发一个基于展示位置的应用程序。 I have used firebase realtime database for this.我为此使用了 firebase 实时数据库。 I am matching company name from the "Job Post" db and "Applied Candidate" db, so that only applied candidates detail for that particular company will be displayed.我正在匹配“职位发布”数据库和“已申请候选人”数据库中的公司名称,因此只会显示该特定公司的已申请候选人详细信息。 Everything is working fine but the issue is the recyclerview's data loads only when the button is clicked the second time.一切正常,但问题是 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);


            }
        }
    }


}

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.假设您的onDataChange确实被调用,成功从快照中读取PostJobData数据,并将其添加到ls ,您不会告诉 Android 列表已更改。 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
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么在 firestore 中添加新数据时 recyclerview 会刷新? - Why does recyclerview refreshed when new data is added in firestore? 单击项目时将数据从 recyclerView 传递到新活动 Kotlin - Pass data from recyclerView to new activity when an item is clicked Kotlin 第一次加载页面时,React useEffect 返回空数据 - React useEffect is returning empty data when page loads for the first time 第一次加载页面时,React useEffect 返回空数据数组 - React useEffect is returning empty data array when page loads for the first time 当我将数据更新到 Firestore 时 RecyclerView 崩溃 - RecyclerView crashes when I update data to Firestore 如何创建单击按钮时可以过滤的表? - How to create a table that can be filtered when a button is clicked? 当引用更改并变为空白时,FirebaseUI RecyclerView 不会更新 - FirebaseUI RecyclerView does not update when reference is changed and goes blank Firebase 仅在第二次或第三次尝试时获取数据 - Firebase only fetches data on second or third attempt 从firebase加载数据时RecyclerView onBindViewHolder()报错 - RecyclerView onBindViewHolder() error when loading data from firebase 当我第二次调用 function 时,Kotlin 流什么也没发出 - Kotlin flow emits nothing when I call the function second time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM