通过设备运行应用程序时,适配器未显示项目

时间:2018-09-21 07:08:16

标签: java android

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.stylist_fragment, container, false);

        bundle=getArguments();
        if(bundle!=null){
            bookingDetails=(BookingDetails)bundle.getParcelable("bookingDetails");
        }

        recyclerView =  view.findViewById(R.id.stylistRecycler);

        try{
            date=bookingDetails.getDate();
            startTime=bookingDetails.getStartTime();
            services=bookingDetails.getServices();
            servicesId=bookingDetails.getServicesId();
            stylistidArray=bookingDetails.getStylistId();
            durationList=bookingDetails.getDuration();
            price=bookingDetails.getPrice();
            endTimeList=bookingDetails.getEndTime();
        }catch (Exception e){
            e.printStackTrace();
        }

        new freeBusy().execute();

        return view;
    }

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

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            stylistDetailsList = new ArrayList<>();


            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(stylistDetailsList);
                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();

        }
    }

}

//这是我的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/tools">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/darkgrey">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

            <android.support.v7.widget.Toolbar
                android:id="@+id/topbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/black">

                <ImageView
                    android:id="@+id/back_Img"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_arrow_back_black_24dp"
                    />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginStart="@dimen/_60sdp"
                    android:textSize="@dimen/_20sdp"
                    android:textColor="@color/white"
                    android:fontFamily="@string/roboto_light"
                    android:text="@string/choose_1_2_services"/>


            </android.support.v7.widget.Toolbar>

            <android.support.v7.widget.CardView
                android:id="@+id/dameklipCard"
                android:layout_width="wrap_content"
                android:layout_height="@dimen/_150sdp"
                android:layout_margin="@dimen/_10sdp"
                android:layout_below="@+id/topbar"
                app:cardCornerRadius="@dimen/_15sdp">
                <android.support.v7.widget.CardView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:cardBackgroundColor="#f5f5ef"
                    card_view:cardElevation="3dp"
                    app:cardCornerRadius="@dimen/_15sdp">

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                        <RelativeLayout
                            android:id="@+id/rounderblue"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/_80sdp">

                            <de.hdodenhof.circleimageview.CircleImageView
                                android:id="@+id/cardImg"
                                android:layout_width="match_parent"
                                android:layout_height="match_parent"
                                android:background="@drawable/card_bg"/>

                            <TextView
                                android:id="@+id/services"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_marginStart="@dimen/_16sdp"
                                android:fontFamily="sans-serif-light"
                                android:textStyle="italic"
                                android:textSize="@dimen/_11sdp"
                                android:textColor="#EAEAEA"
                                android:layout_marginTop="@dimen/_16sdp"
                                android:text="@string/services"/>

                            <TextView
                                android:id="@+id/serviceTv"
                                android:layout_width="@dimen/_149sdp"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/services"
                                android:fontFamily="@string/roboto_genraltext"
                                android:layout_marginStart="@dimen/_16sdp"
                                android:textSize="@dimen/_20sdp"
                                android:textColor="#EAEAEA"
                                android:text=""/>

                            <TextView
                                android:id="@+id/durationTv"
                                android:layout_width="wrap_content"
                                android:layout_height="wrap_content"
                                android:layout_below="@+id/serviceTv"
                                android:layout_marginStart="@dimen/_16sdp"
                                android:fontFamily="@string/roboto_genraltext"
                                android:textSize="@dimen/_11sdp"
                                android:textColor="#FFFFFF"
                                android:text=""/>
                        </RelativeLayout>

                        <de.hdodenhof.circleimageview.CircleImageView
                            android:id="@+id/stylistImg"
                            android:layout_width="@dimen/_90sdp"
                            android:layout_height="@dimen/_90sdp"
                            android:layout_marginTop="@dimen/_16sdp"
                            android:layout_marginEnd="@dimen/_30sdp"
                            android:src="#90eeeded"
                            android:layout_alignParentEnd="true"
                            />

                        <TextView
                            android:id="@+id/stylistNameTv"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/stylistImg"
                            android:layout_alignParentEnd="true"
                            android:fontFamily="@string/roboto_bold"
                            android:textSize="@dimen/_11sdp"
                            android:layout_marginTop="@dimen/_5sdp"
                            android:layout_marginEnd="@dimen/_40sdp"
                            android:textColor="@color/black"
                            android:layout_marginBottom="@dimen/_10sdp"
                            android:text="" />

                        <TextView
                            android:id="@+id/timeTV"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/rounderblue"
                            android:fontFamily="@string/roboto_light"
                            android:layout_marginStart="@dimen/_16sdp"
                            android:textColor="#383838"
                            android:layout_marginTop="@dimen/_5sdp"
                            android:text="@string/your_appointment_date_time"
                            android:textSize="@dimen/_11sdp"
                            android:textStyle="italic" />

                        <TextView
                            android:id="@+id/time"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/timeTV"
                            android:layout_marginStart="@dimen/_16sdp"
                            android:fontFamily="@string/roboto_medium"
                            android:text="Kl."
                            android:textSize="@dimen/_18sdp"/>

                        <TextView
                            android:id="@+id/timeTv"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/timeTV"
                            android:layout_marginStart="@dimen/_5sdp"
                            android:fontFamily="@string/roboto_medium"
                            android:layout_toEndOf="@+id/time"
                            android:text=""
                            android:textSize="@dimen/_18sdp"/>

                        <TextView
                            android:id="@+id/dateTv"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_below="@+id/timeTv"
                            android:layout_marginStart="@dimen/_16sdp"
                            android:fontFamily="@string/roboto_medium"
                            android:text=""
                            android:textSize="@dimen/_18sdp"
                            />
                    </RelativeLayout>

                </android.support.v7.widget.CardView>

            </android.support.v7.widget.CardView>

            <TextView
                android:id="@+id/chooseTv"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@string/choose_one"
                android:layout_marginStart="@dimen/_25sdp"
                android:layout_below="@+id/dameklipCard"
                />

            <android.support.v7.widget.RecyclerView
                android:id="@+id/stylistRecycler"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/chooseTv"
                />

        </RelativeLayout>


    </ScrollView>


</RelativeLayout>

这里我正在从Firebase获取详细信息并在recyclerview中填充所有值。问题是当我调试应用程序时,值正在recyclerview中填充,但是当我运行应用程序时,值未在recyclerview中填充我也尝试打印logcat中的值。这些值在logcat中可用

1 个答案:

答案 0 :(得分:1)

问题与asyncTask有关,asyncTask在从Firebase加载项目之前已完成。您甚至不需要asyncTask。请尝试以下

NewStylistAdapter newStylistAdapter;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view= inflater.inflate(R.layout.stylist_fragment, container, false);

    bundle=getArguments();
    if(bundle!=null){
        bookingDetails=(BookingDetails)bundle.getParcelable("bookingDetails");
    }

    recyclerView =  view.findViewById(R.id.stylistRecycler);
    try{
        date=bookingDetails.getDate();
        startTime=bookingDetails.getStartTime();
        services=bookingDetails.getServices();
        servicesId=bookingDetails.getServicesId();
        stylistidArray=bookingDetails.getStylistId();
        durationList=bookingDetails.getDuration();
        price=bookingDetails.getPrice();
        endTimeList=bookingDetails.getEndTime();
    }catch (Exception e){
        e.printStackTrace();
    }

    loadData();

    return view;
}

private void loadData(){
   setupAdapter();
   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);
                        newStylistAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }
}

private void setupAdapter(){
   stylistDetailsList = new ArrayList<>();
   newStylistAdapter = new NewStylistAdapter(stylistDetailsList);
   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);
}