For循环不适用于我的查询Firestore

时间:2018-11-13 12:46:24

标签: java android google-cloud-firestore

我被困在一个项目中,我需要从集合中的文档中获取数据。我目前正在将Firestore用于Android Studio。事实是,我的查询在task.isSuccessful之后显示了吐司,但是For循环没有从云中获取数据,甚至没有显示我放入循环中的测试Toast。一切正常,直到进入For循环。 RecyclerView仅在我手动设置参数时显示内容  进行测试,因此Adapter可以正常工作,但是当我尝试显示Firestorm中的数据时,它什么也没有显示!任何想法?它没有显示任何错误!

public class MostrarConteudoFragment extends AppCompatActivity {

    RecyclerView recyclerView2;
    List<Compras> listaCompras = new ArrayList<>();
    Compras compras;
    FirebaseFirestore db;
    FirebaseUser user;
    FirebaseAuth mAuth;
    SavedFragment savedFragment;
    public String idUpdate = "";  // id do item para o update


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mostrar_conteudo_fragment);


        db = FirebaseFirestore.getInstance();
        mAuth = FirebaseAuth.getInstance( );
        user = mAuth.getCurrentUser( );
        recyclerView2 = findViewById(R.id.recyclerView2);
        savedFragment = new SavedFragment();


    }

     protected void onStart(){
    super.onStart();

    if(listaCompras.size() > 0)
        listaCompras.clear();

    db.collection("Usuario").document(user.getUid()).collection("ItensSalvos")
            .whereEqualTo("id", idUpdate).get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {

                    if(task.isSuccessful()) {

                        Toast.makeText(MostrarConteudoFragment.this, "task succcessful", Toast.LENGTH_SHORT).show( );

                        for (QueryDocumentSnapshot doc : task.getResult( )) {

                            Toast.makeText(MostrarConteudoFragment.this, "doc succcessful", Toast.LENGTH_SHORT).show( );

                            compras = new Compras(doc.getString("id"),
                                    doc.getString("inputNome"),
                                    doc.getString("inputQtd"),
                                    doc.getString("inputPreco"),
                                    doc.getBoolean("checkBoxOk"));

                            listaCompras.add(compras);

                        }

                        //Configurar adapter
                        AdapterMostrarConteudo mostrarConteudo = new AdapterMostrarConteudo(MostrarConteudoFragment.this, listaCompras);


                        //Configurar RecyclerView
                        RecyclerView.LayoutManager layoutManager1 = new LinearLayoutManager(getApplicationContext( ));
                        recyclerView2.setLayoutManager(layoutManager1);
                        recyclerView2.setHasFixedSize(true);
                        recyclerView2.setAdapter(mostrarConteudo);
                    }   
                }
            });



}

}

公共类AdapterSave扩展了RecyclerView.Adapter {

List<DataHora> listaDataHora;
SavedFragment savedFragment;
MostrarConteudoFragment mostrarConteudo;


public AdapterSave(SavedFragment savedFragment, List<DataHora> listaDataHora) {
    this.savedFragment = savedFragment;
    this.listaDataHora = listaDataHora;
}

@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

    View itemLista = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.lista_salvas, viewGroup, false);

    mostrarConteudo = new MostrarConteudoFragment();

    return new MyViewHolder(itemLista);
}

@Override
public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {

    myViewHolder.data.setText(listaDataHora.get(i).getData());
    myViewHolder.hora.setText(listaDataHora.get(i).getHora());

    myViewHolder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position, boolean isLongClick) {

            mostrarConteudo.idUpdate = listaDataHora.get(position).getId();

            Intent intent = new Intent(savedFragment.getActivity(), MostrarConteudoFragment.class);
            savedFragment.startActivity(intent );

            Toast.makeText(savedFragment.getContext(), mostrarConteudo.idUpdate, Toast.LENGTH_SHORT).show( );



        }
    });

}

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

public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener, View.OnCreateContextMenuListener {

    TextView data, hora;
    ItemClickListener itemClickListener;

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

        itemView.setOnClickListener(this);
        itemView.setOnCreateContextMenuListener(this);

        data = itemView.findViewById(R.id.textData);
        hora = itemView.findViewById(R.id.textHora);

    }

    // Metodo construtor da interface ItemClickListener
    public void setItemClickListener(ItemClickListener itemClickListener) {
        this.itemClickListener = itemClickListener;


    }

    @Override     // Permite abrir o menu com o click
    public void onClick(View view) {
        itemClickListener.onClick(view, getAdapterPosition(), false);

    }


    @Override        // Cria o menu quando item é clicado
    public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {


    }
}

}

enter image description here

enter image description here

0 个答案:

没有答案