Android for循环执行后如何调用适配器

时间:2018-09-17 07:15:08

标签: java android

  private class freeBusy extends AsyncTask<Void, Void, Void> {
    @Override
    protected void onPreExecute() {
        super.onPreExecute();

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        for(int i=0;i<stylistidArray.size();i++){
            FirebaseFirestore db= FirebaseFirestore.getInstance();
            db.collection("stylists").document(stylistidArray.get(i)).get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                @Override
                public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                    if(task.isSuccessful()){
                        DocumentSnapshot documentSnapshot=task.getResult();
                        if(documentSnapshot.exists()){
                            String stylistid=documentSnapshot.getId();
                            String firstName=documentSnapshot.getString("firstName");
                            String lastName=documentSnapshot.getString("lastName");
                            String imageUrl=documentSnapshot.getString("imageUrl");
                            StylistCollection stylistCollection=new StylistCollection(stylistid,firstName,lastName,imageUrl);
                            stylistDetailsList.add(stylistCollection);
                        }
                    }
                }
            });
        }



        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        NewStylistAdapter   newStylistAdapter=new NewStylistAdapter(date,startTime,servicesId,price,stylistidArray,durationList,endTimeList,stylistDetailsList,stylistImg);
        RecyclerView.ItemDecoration dividerItemDecoration = new DividerItemDecorator(ContextCompat.getDrawable(getContext(), R.drawable.divider));
        recyclerView.addItemDecoration(dividerItemDecoration);
        recyclerView.setAdapter(newStylistAdapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);
        newStylistAdapter.notifyDataSetChanged();

    }
 }

我有stylistidArray,我想在满足我的for循环条件后调用适配器。在这里,我试图在for循环上次执行时调用适配器,但在启动自身时执行if语句不起作用。

1 个答案:

答案 0 :(得分:0)

您只需要全局创建Adapter的对象,就可以像下面这样访问所有对象

 YOUR_ADAPTER mAdapter;
   @Override
    protected void onCreate(Bundle savedInstanceState) {

   }

并在每当您在列表中进行更改时通知您,如下所示

  ArrayList<String> YOUR_ARRAYLIST = new ArrayList<>();
        for (int i = 0; i < YOUR_ARRAYLIST.size(); i++) {
            YOUR_ARRAYLIST.add(String.valueOf(i));
        }
        /**
         * After populating the data on ArrayList than u need to update OR intilaize the  Recyclview
         */


        if (mAdapter!=null) {
            mAdapter = new YOUR_ADAPTER(this, YOUR_ARRAYLIST);
            RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
            YOUR_RECYCLVIEW.setLayoutManager(mLayoutManager);
            YOUR_RECYCLVIEW.setItemAnimator(new DefaultItemAnimator());
            YOUR_RECYCLVIEW.setAdapter(mAdapter);

        }
        else
        {
            mAdapter.notifyDataSetChanged();
        }