Como pegar os dados de um item do RecyclerView ao clicar nele usando Firebase Firestore?

Firebase

29/06/2019

Boa tarde....
Estou trabalhando em meu TCC e estou desenvolvendo uma app de controle de pedidos no Android Studio usando Java e Firetore, estou com uma dúvida sobre como pegar os dados de um produto na ao clicar na RecycleView, consigo pegar a posição e ID do produto no Firestore.

Classe Adapter:

public class ProductAdapter extends FirestoreRecyclerAdapter<Product, ProductAdapter.ProductHolder> {

    private OnItemClickListener listener;

    public ProductAdapter(@NonNull FirestoreRecyclerOptions<Product> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull ProductHolder holder, int position, @NonNull Product model) {
        holder.mTextDescription.setText(model.getDescription());
        holder.mTextMeasure.setText(model.getMeasure());
        holder.mTextWeight.setText(String.valueOf(model.getWeight()));
        holder.mTextQuantity.setText(String.valueOf(model.getQuantity()));
        holder.mTextSalePrice.setText(String.valueOf(model.getSalePrice()));
        holder.mTextExpirationDate.setText(model.getExpirationDate());

    }

    @NonNull
    @Override
    public ProductHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.row_product_list,
                viewGroup, false);

        return new ProductHolder(view);
    }

    class ProductHolder extends RecyclerView.ViewHolder {

        TextView mTextDescription;
        TextView mTextMeasure;
        TextView mTextWeight;
        TextView mTextQuantity;
        TextView mTextSalePrice;
        TextView mTextExpirationDate;


        public ProductHolder(@NonNull View itemView) {
            super(itemView);
            this.mTextDescription = itemView.findViewById(R.id.textView_product_description);
            this.mTextMeasure = itemView.findViewById(R.id.textView_product_measure);
            this.mTextWeight = itemView.findViewById(R.id.textView_product_weight);
            this.mTextQuantity = itemView.findViewById(R.id.textView_product_quantity);
            this.mTextSalePrice = itemView.findViewById(R.id.textView_product_salePrice);
            this.mTextExpirationDate = itemView.findViewById(R.id.textView_product_expirationDate);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    int position = getAdapterPosition();
                    if (position != RecyclerView.NO_POSITION && listener != null) {
                        listener.onItemClick(getSnapshots().getSnapshot(position), position);
                    }
                }
            });
        }
    }

    public interface OnItemClickListener {
        void onItemClick(DocumentSnapshot documentSnapshot, int position);

    }

    public void setOnItemClickListener(OnItemClickListener listener) {

        this.listener = listener;

    }

}


E a Activity que lista os itens na RecycleView:

public class ProductListActivity extends AppCompatActivity {

    //firestore instance
    private FirebaseFirestore db = ConfigurationFirebase.getFirebaseFirestore();
    private CollectionReference productRef = db.collection("products");

    private static final String TAG = "ProductListActivity";

    private ProductAdapter productAdapter;

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

        setUpRecycleView();
    }

    private void setUpRecycleView() {

        Query query = productRef.orderBy("description", Query.Direction.ASCENDING);

        FirestoreRecyclerOptions<Product> optionsProduct = new FirestoreRecyclerOptions.Builder<Product>()
                .setQuery(query, Product.class)
                .build();

        productAdapter = new ProductAdapter(optionsProduct);

        RecyclerView recyclerView = findViewById(R.id.recycler_view_listProducts);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(productAdapter);


        productAdapter.setOnItemClickListener(new ProductAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(DocumentSnapshot documentSnapshot, int position) {
                Product product = documentSnapshot.toObject(Product.class);
                String id = documentSnapshot.getId();
                String path = documentSnapshot.getReference().getPath();

              //  DocumentReference docRef = db.collection("products").document(id);
             //   docRef.update("description", "Pastel de Queijo");

                Toast.makeText(ProductListActivity.this, "Position: " + position + "ID: " + id, Toast.LENGTH_SHORT).show();

                openProductDetails();

            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        productAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        productAdapter.stopListening();
    }

    public void openProductDetails() {
        Intent intent = new Intent(ProductListActivity.this, ProductDetailsActivity.class);
        startActivity(intent);
        finish();
    }

}

Criei uma Activity ProductDetails para mostrar as informações dos produtos. Ela não foi implementada, mas ela é chamada na RecycleView quando um item é selecionado e os dados não parecem ainda.
Adriano Guimarães

Adriano Guimarães

Curtidas 0
POSTAR