Firestore不从文档中检索某些字段

时间:2018-05-03 18:18:58

标签: java android firebase google-cloud-firestore

我在片段中有一个回收器来显示来自firestore中集合的文档,问题是,它只是从文档中检索某些字段,看起来它忽略了模型类中的其他一些Setter字段,是我得到的错误:

  

W / Firestore :( 0.6.6-dev)[zzko]:找不到sabado的setter / field   类   com.example.denilsondasilva.iasdmt.com.example.denilsondasilva.com.example.denilsondasilva.MODELS.ModelProgramas

现在这是我的模特课:

public class ModelProgramas {

    public ModelProgramas() {
    }

    public ModelProgramas(String sabado, String evento, String responsavel, String data, String horario) {
        this.sabado = sabado;
        this.evento = evento;
        this.responsavel = responsavel;
        this.data = data;
        this.horario = horario;
    }

    public String getSabado() {
        return sabado;
    }

    public void setSabado(String sabado) {
        this.sabado = sabado;
    }

    public String getEvento() {
        return evento;
    }

    public void setEvento(String evento) {
        this.evento = evento;
    }

    public String getResponsavel() {
        return responsavel;
    }

    public void setResponsavel(String responsavel) {
        this.responsavel = responsavel;
    }

    public String getData() {
        return data;
    }

    public void setData(String data) {
        this.data = data;
    }

    public String getHorario() {
        return horario;
    }

    public void setHorario(String horario) {
        this.horario = horario;
    }

    private String sabado;
    private String evento;
    private String responsavel;
    private String data;
    private String horario;
}

我已经为类创建了一个EMPTY CONSTRUCTOR,我为变量以及Getters和Setter创建了CONSTRUCTORS,我已经看到了以前的问题同样的问题,所以我知道我的GETTERS和SETTERS以及变量是正确创建的。变量以大写字母开头的小写,getter和setter。并且firestore文档中的字符串与创建的变量

相同

这是我的适配器:

public class PROGRAMASrecyclerAdapter extends RecyclerView.Adapter<PROGRAMASrecyclerAdapter.Programasholder> {

    Context ctx;
    private List<ModelProgramas> Progs_list;

    public PROGRAMASrecyclerAdapter(List<ModelProgramas> progs_list) {
        this.Progs_list = progs_list;
    }

    @NonNull
    @Override
    public Programasholder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ctx = parent.getContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.programas_list_item,parent,false);
        return new Programasholder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Programasholder holder, int position) {

        final String sabado = Progs_list.get(position).getSabado();
        holder.setSabado(sabado);

        String evento = Progs_list.get(position).getEvento();
        holder.setEvento(evento);

        String responsavel = Progs_list.get(position).getResponsavel();
        holder.setResponsavel(responsavel);

        String data = Progs_list.get(position).getData();
        holder.setData(data);

        String horario = Progs_list.get(position).getHorario();
        holder.setHorario(horario);

        holder.mcardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(ctx, sabado, Toast.LENGTH_SHORT).show();
            }
        });
    }

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

    public class Programasholder extends RecyclerView.ViewHolder{
        View mView;
        private TextView Sabadotxt;
        private TextView Eventotxt;
        private TextView Responsaveltxt;
        private TextView DatadoEventotxt;
        private TextView Horariotxt;
        private CardView mcardView;

        public Programasholder(View itemView) {
            super(itemView);
            mView = itemView;

            mcardView = mView.findViewById(R.id.listCard_Programas);
        }

        public void setSabado(String sabado){
            Sabadotxt = mView.findViewById(R.id.Prog_sabadotxt);
            Sabadotxt.setText(sabado);
        }

        public void setEvento(String evento){
            Eventotxt = mView.findViewById(R.id.Prog_eventotxt);
            Eventotxt.setText(evento);
        }

        public void setResponsavel(String responsavel){
            Responsaveltxt = mView.findViewById(R.id.Prog_responsaveltxt);
            Responsaveltxt.setText(responsavel);
        }

        public void setData(String data){
            DatadoEventotxt = mView.findViewById(R.id.Prog_datadoEventotxt);
            DatadoEventotxt.setText(data);
        }

        public void setHorario(String horario){
            Horariotxt = mView.findViewById(R.id.Prog_horariotxt);
            Horariotxt.setText(horario);
        }
    }
}

我甚至在cardview上创建了一个on click监听器来显示检索到的字符串&#34; sabado&#34;查看问题是否在Viewholder类上,但它是RETURNING NULL

这是我的片段类:

public class ProgramasFragment extends Fragment {

    View view;
    private RecyclerView recyclerView;
    private List<ModelProgramas> Progs_List;
    private FirebaseFirestore firebaseFirestore;
    PROGRAMASrecyclerAdapter programaSrecyclerAdapter;


    public ProgramasFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_programas, container, false);
        Progs_List = new ArrayList<>();
        programaSrecyclerAdapter = new PROGRAMASrecyclerAdapter(Progs_List);

        firebaseFirestore = FirebaseFirestore.getInstance();

        recyclerView = view.findViewById(R.id.Recycler_ProgramasList);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        recyclerView.setAdapter(programaSrecyclerAdapter);

        firebaseFirestore.collection("programasdatarde").addSnapshotListener(new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

                if (queryDocumentSnapshots == null){
                    String error = e.getCause().getMessage();
                    Toast.makeText(getActivity().getApplicationContext(), "Empty QUERY: "+error, Toast.LENGTH_SHORT).show();
                }else{

                    for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){

                        if (doc.getType() == DocumentChange.Type.ADDED){

                            ModelProgramas modelProgramas = doc.getDocument().toObject(ModelProgramas.class);
                            Progs_List.add(modelProgramas);
                            programaSrecyclerAdapter.notifyDataSetChanged();
                        }
                    }
                }
            }
        });

        return view;
    }
}

我知道问题不在片段类和适配器中,我的意思是我想,因为我一遍又一遍地审阅它们,并且我有其他片段与recyclerviews执行相同的功能,他们不提供这种错误。

请不要将此标记为重复问题,因为我已经解决了其他相关位置上的所有其他解决方案,但它没有解决我的错误!请帮助。

数据结构是这样的。

enter image description here

1 个答案:

答案 0 :(得分:0)

我发现您的代码出现了错误和一些改进,如下所示。

删除recyclerView.setHasFixedSize(true);。因为您的RecyclerView是动态加载的,并且您的每个元素可能没有固定的高度。

不要在for循环中调用notifyDataSetChanged。像这样做。

firebaseFirestore.collection("programasdatarde").addSnapshotListener(new EventListener<QuerySnapshot>() {
    @Override
    public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots, @Nullable FirebaseFirestoreException e) {

        if (queryDocumentSnapshots == null){
            String error = e.getCause().getMessage();
            Toast.makeText(getActivity().getApplicationContext(), "Empty QUERY: "+error, Toast.LENGTH_SHORT).show();
        } else {
            for (DocumentChange doc: queryDocumentSnapshots.getDocumentChanges()){
                if (doc.getType() == DocumentChange.Type.ADDED){
                    ModelProgramas modelProgramas = doc.getDocument().toObject(ModelProgramas.class);
                    Progs_List.add(modelProgramas);
                }
            }

            // Move the notify data set out here. 
            programaSrecyclerAdapter.notifyDataSetChanged();
        }
    }
});

现在简化您的模型类。我认为你不需要明确的getter和setter。只是将变量声明为公共变量即可。然而,吸气剂和制定者是可以的。我在那里找不到任何错误。

public class ModelProgramas {

    public String sabado;
    public String evento;
    public String responsavel;
    public String data;
    public String horario;

    public ModelProgramas() {}
}

请进行更改并检查错误是否仍然存在。但是,我发现您的适配器和模型类没有任何问题。我尝试放在这里的建议是将notifyDataSetChanged移出循环并从setHasFixedSize中删除RecyclerView属性。