getSnapshots()不适用于Firestore分页适配器

时间:2020-11-05 20:43:48

标签: android firebase

我是android开发人员,无法解决getSnapshots()对于分页适配器不起作用的问题,尽管它对于回收站适配器来说也很好。

    package com.applex.spectrumbiocides;

import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

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

import com.firebase.ui.firestore.FirestoreRecyclerAdapter;
import com.firebase.ui.firestore.FirestoreRecyclerOptions;
import com.firebase.ui.firestore.paging.FirestorePagingAdapter;
import com.firebase.ui.firestore.paging.FirestorePagingOptions;
import com.firebase.ui.firestore.paging.LoadingState;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestoreException;
import com.squareup.picasso.Picasso;

import java.util.Objects;

public class InventoryFirestoreAdaptor extends FirestorePagingAdapter<Inventory, InventoryFirestoreAdaptor.Products_view_holder> {

    private onItemClickListener listener;


    /*
     *
     * Create a new RecyclerView adapter that listens to a Firestore Query.  See {@link
     * FirestoreRecyclerOptions} for configuration options.
     *
     * @param options
     */
    public InventoryFirestoreAdaptor(@NonNull FirestorePagingOptions<Inventory> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull Products_view_holder holder, int position, @NonNull Inventory model) {

        holder.item_name.setText(model.getName());
        holder.item_description.setText("Description: " + model.getDescription());
        holder.item_price.setText("Price: ₹" + model.getPrice());
        holder.item_discount.setText("Discount: " + model.getDiscount() + " %");
        holder.item_stock.setText("Stocks available: " + model.getStock());

        //holder.item_img.setImageURI(Uri.parse(model.getImage_URL()));

        //adding the image.

        Picasso.get()
                .load(model.getImage_URL())
                .fit()
                .centerCrop()
                .into(holder.item_img);

    }

    @NonNull
    @Override
    public Products_view_holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.inventory_single_item, parent ,false);
        return new Products_view_holder(view);

    }

    /*
    @Override
    public void onError(FirebaseFirestoreException e){
        Log.e("error", Objects.requireNonNull(e.getMessage()));
    }

     */





    // view holder class.....
    public class Products_view_holder extends RecyclerView.ViewHolder {

        private final TextView item_name;
        //private TextView item_category;
        private final TextView item_description;
        private final TextView item_price;
        private final TextView item_discount;
        private final TextView item_stock;
        private final ImageView item_img;



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

            //item_name = itemView.findViewById(R.id.iname);

            item_name = itemView.findViewById(R.id.item_name);
            //item_category = findViewById(R.id.item_category);
            item_description = itemView.findViewById(R.id.item_description);
            item_price = itemView.findViewById(R.id.item_price);
            item_discount = itemView.findViewById(R.id.item_discount);
            item_stock = itemView.findViewById(R.id.item_stock);

            item_img = itemView.findViewById(R.id.item_image);


            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);
                    }

                    //To sent a click from the adapter class to the underling activity an interface needs to be declared!!
                }
            });
        }
    }


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

    public void setOnItemCLickListener(onItemClickListener listener){
        this.listener = listener;
    }


}

它只是说无法解析getSnapshot()!!!

错误:找不到符号 listener.onItemClick(getSnapshots()。getSnapshot(position),position); ^ 符号:方法getSnapshots()

1 个答案:

答案 0 :(得分:0)

哦,我刚刚解决了它!

代替:

listener.onItemClick(getSnapshots().getSnapshot(position),position);

它必须是:

listener.onItemClick(getItem(getAdapterPosition()), position);

上一行虽然可以很好地获取简单的回收站适配器的文档快照。 ?