Recyclerview不会将整个收藏集从firebase加载到网格中,并且Picasso不会加载任何图片

时间:2019-06-19 18:08:39

标签: android firebase gridview android-recyclerview picasso

我有2个问题:

我的回收商成功获取了我在Firebase Firestore中具有的类别的名称,但是:

1,除非我更改跨度计数以完全匹配相同数量的项目,否则它不会加载整个集合。

2毕加索未能加载收藏夹中的图片。

我曾尝试更改回收站的高度和cardviews,但无法正常工作。似乎唯一可行的方法是更改​​跨度计数,但由于它们全部卡在1行中,因此一切都被压缩了。

关于图像加载,我尝试了其他问题的其他解决方案,但仍然没有。我在日志中遇到的唯一错误是“ E / RecyclerView:未连接适配器;正在跳过布局”

我的家碎片


import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.buzzamaid.Adapter.ExtrasAdapter;
import com.example.buzzamaid.Common.SpacesItemDecoration;
import com.example.buzzamaid.Interface.BathroomsLoadListener;
import com.example.buzzamaid.Interface.ExtraServicesLoadListerner;
import com.example.buzzamaid.Model.Extras;
import com.example.buzzamaid.R;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.CollectionReference;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;
import com.jaredrummler.materialspinner.MaterialSpinner;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;
import dmax.dialog.SpotsDialog;

public class HomeDetailsFragment  extends Fragment implements BathroomsLoadListener, ExtraServicesLoadListerner {

    //Variable
    CollectionReference bedroomsRef;
    CollectionReference bathroomsRef;
   CollectionReference extrasRef;


    BathroomsLoadListener bathroomsLoadListener;
    HomeDetailsFragment bedroomsLoadListener;
    ExtraServicesLoadListerner extraServicesLoadListerner;


    @BindView(R.id.spinner)
    MaterialSpinner spinner;

    @BindView(R.id.spinner_bedrooms)
    MaterialSpinner spinner_bedrooms;


    @BindView(R.id.recycler_bedrooms)
    RecyclerView recycler_bedrooms;

    @BindView(R.id.extra_services_recycler)
    RecyclerView extra_services_recycler;


    Unbinder unbinder;
    AlertDialog dialog;




    static HomeDetailsFragment instance;

    public  static HomeDetailsFragment getInstance() {
        if (instance == null)
            instance = new HomeDetailsFragment();
        return instance;

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bathroomsRef = FirebaseFirestore.getInstance().collection("AllServices/HomeCleaning/Bathrooms");
        bedroomsRef = FirebaseFirestore.getInstance().collection("AllServices/HomeCleaning/Bedrooms");



        bathroomsLoadListener = this;
        bedroomsLoadListener = this;
        extraServicesLoadListerner = this;

        dialog = new SpotsDialog.Builder().setContext(getActivity()).build();

    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
       super.onCreateView(inflater, container, savedInstanceState);

       View itemView =  inflater.inflate(R.layout.fragment_home_details,container,false);
        unbinder = ButterKnife.bind(this,itemView);

        initView();
        LoadBathrooms();
        LoadBedrooms();


        return itemView;


    }

    private void initView() {
        extra_services_recycler.addItemDecoration(new SpacesItemDecoration(4));
        extra_services_recycler.setLayoutManager(new GridLayoutManager(getActivity(), 3));

    }


    private void LoadBedrooms() {
        bedroomsRef.get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful())
                        {
                            List<String> list = new ArrayList<>();
                            list.add("Select number of bedrooms");
                            for (QueryDocumentSnapshot documentSnapshot:task.getResult())
                                list.add(documentSnapshot.getId());
                            bedroomsLoadListener.onBedroomLoadListenerSuccess(list);
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                bedroomsLoadListener.onBedroomLoadListenerFailed(e.getMessage());
            }
        });
    }

    private void onBedroomLoadListenerSuccess(List<String> numberbedroomList) {
        spinner_bedrooms.setItems(numberbedroomList);
        spinner_bedrooms.setOnItemSelectedListener(new MaterialSpinner.OnItemSelectedListener() {
            @Override
            public void onItemSelected(MaterialSpinner view, int position, long id, Object item) {
                if (position  > 0)
                {
                    loadExtraServices(item.toString());
                }
            }
        });

    }

    private void loadExtraServices(String extrasNames) {
        dialog.show();

        extrasRef = FirebaseFirestore.getInstance()
                .collection("AllServices/HomeCleaning/ExtraServices");

        extrasRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull Task<QuerySnapshot> task) {
                List<Extras> list = new ArrayList<>();
                if (task.isSuccessful())
                {
                    for (QueryDocumentSnapshot documentSnapshot:task.getResult())
                        list.add(documentSnapshot.toObject(Extras.class));
                    extraServicesLoadListerner.onExtraServicesLoadListenerSuccess(list);
                }
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                extraServicesLoadListerner.onExtraServicesLoadListenerFailed(e.getMessage());
            }
        });
    }

    private void onBedroomLoadListenerFailed(String message) {
        Toast.makeText(getActivity(),message,Toast.LENGTH_LONG);
    }

    private void LoadBathrooms() {
        bathroomsRef.get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful())
                        {
                           List<String> list = new ArrayList<>();
                           list.add("Select number of bathrooms");
                           for (QueryDocumentSnapshot documentSnapshot:task.getResult())
                               list.add(documentSnapshot.getId());
                           bathroomsLoadListener.onBathroomLoadListenerSuccess(list);
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
              bathroomsLoadListener.onBathroomLoadListenerFailed(e.getMessage());
            }
        });
    }


    @Override
    public void onBathroomLoadListenerSuccess(List<String> numberbathroomsList) {
spinner.setItems(numberbathroomsList);
    }

    @Override
    public void onBathroomLoadListenerFailed(String message) {
        Toast.makeText(getActivity(),message,Toast.LENGTH_LONG).show();

    }


    @Override
    public void onExtraServicesLoadListenerSuccess(List<Extras> ExtrasList) {
        ExtrasAdapter adapter = new ExtrasAdapter(getActivity(),ExtrasList);
        extra_services_recycler.setAdapter(adapter);
        dialog.dismiss();

    }

    @Override
    public void onExtraServicesLoadListenerFailed(String message) {
        Toast.makeText(getActivity(),message,Toast.LENGTH_LONG).show();
        dialog.dismiss();

    }
}

我的适配器


import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.buzzamaid.Model.Extras;
import com.example.buzzamaid.R;
import com.squareup.picasso.Picasso;

import java.util.List;

public class ExtrasAdapter extends RecyclerView.Adapter<ExtrasAdapter.MyViewHolder> {

    TextView extra_services_title;
    ImageView extra_services_image;

    Context context;
    List<Extras> extrasList;

    public ExtrasAdapter(Context context, List<Extras> extrasList) {
        this.context = context;
        this.extrasList = extrasList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View itemView = LayoutInflater.from(context)
                .inflate(R.layout.extra_services_card,viewGroup,false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
       myViewHolder.extra_services_title.setText(extrasList.get(i).getName());
       Picasso.get().load(extrasList.get(i).getImage()).into(extra_services_image);





    }

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

    public class MyViewHolder extends RecyclerView.ViewHolder{
        TextView extra_services_title;
        ImageView extra_services_image;

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

            extra_services_title = (TextView)itemView.findViewById(R.id.extra_services_title);
            extra_services_image = (ImageView) itemView.findViewById(R.id.extra_services_image);
        }
    }
}

我的构造者和吸气剂


public class Extras {
    private String name,image;
    private int price;


    public Extras() {
    }

    public Extras(String name, String image, int price) {
        this.name = name;
        this.image = image;
        this.price = price;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }
}

我的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:padding="8dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
   <com.jaredrummler.materialspinner.MaterialSpinner
       android:id="@+id/spinner"
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:textSize="18sp"
       android:fontFamily="@font/abel_regular"/>

    <com.jaredrummler.materialspinner.MaterialSpinner
        android:id="@+id/spinner_bedrooms"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textSize="18sp"
        android:fontFamily="@font/abel_regular"/>

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_bedrooms"
        android:layout_marginTop="15dp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        />

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/extra_services_recycler"
        />



</LinearLayout>

    </ScrollView>


</LinearLayout>

编辑: 数据库结构 Database structure

我希望将图像加载到视图中,并希望加载整个集合,而不仅仅是跨度计数。

1 个答案:

答案 0 :(得分:0)

弄清楚了,回收者没有显示所有cardview的原因是因为我已将card模板设置为匹配父宽度和高度,而不是包装内容。并忘记了“ myviewadapter”到毕加索的(myviewadapter.Destination)