0

im developing a Rent car app uising firestore database when i use simple adapter it fetch data correctly from firestore but when i use FirestoreRecyclerAdapter the app crashes. I badly need to use the FirestoreRecyclerAdapter because i want to get the car document id in adapter and pass it to booing activity when user clicks on book now button.

My app crashes when i use FirestoreRecyclerAdapter and nothing happens in the logcat so unable to find the error.

is there any alternate to pass currennt document id in adapter i watched so many tutorials and go through lot of articles but nothing works in my case here is my adapter

public class CarListAdapter extends FirestoreRecyclerAdapter<CarRentModel, CarListAdapter.RentCarViewHolder> {
    private Context context;

    public CarListAdapter(@NonNull FirestoreRecyclerOptions<CarRentModel> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull RentCarViewHolder holder, int position, @NonNull CarRentModel model) {

        holder.binding.nameCar.setText(model.getCarName());
        holder.binding.carPrice.setText("PKR" + model.getCarPrice());
        Glide.with(context)
                .load(model.getCarImage())
                .into(holder.binding.imageCar);

        if (model.getCarStatus() != 1) {
            holder.binding.buttonRental.setText("Busy");
            holder.binding.buttonRental.setEnabled(false);
        }

        if (model.isWithDriver()) {
            holder.binding.withDriver.setText("With Driver");
        } else {
            holder.binding.withDriver.setText("Without Driver");
        }

    }

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


    public class RentCarViewHolder extends RecyclerView.ViewHolder{
        ActivityItemCarHomepageBinding binding;
        public RentCarViewHolder(@NonNull View itemView) {
            super(itemView);
            binding = ActivityItemCarHomepageBinding.bind(itemView);
        }
    }
}

My model class code is here

public class CarRentModel {
    private String carName, carImage, carColor, carBrand, carLicensePlates, carDescription, carSeat;
    private long carPrice;
    private int carStatus = 1;
    private boolean withDriver ;

    public CarRentModel() {
    }

    public CarRentModel(String carName, String carImage, String carColor, String carBrand, String carLicensePlates, String carDescription, String carSeat, long carPrice, int carStatus) {
        this.carName = carName;
        this.carImage = carImage;
        this.carColor = carColor;
        this.carBrand = carBrand;
        this.carLicensePlates = carLicensePlates;
        this.carDescription = carDescription;
        this.carSeat = carSeat;
        this.carPrice = carPrice;
        this.carStatus = carStatus;
    }

    public CarRentModel(boolean withDriver) {
        this.withDriver = withDriver;
    }

    public boolean isWithDriver() {
        return withDriver;
    }

    public void setWithDriver(boolean withDriver) {
        this.withDriver = withDriver;
    }

   
    public String getCarName() {
        return carName;
    }

    public void setCarName(String carName) {
        this.carName = carName;
    }

    public String getCarImage() {
        return carImage;
    }

    public void setCarImage(String carImage) {
        this.carImage = carImage;
    }

    public String getCarColor() {
        return carColor;
    }

    public void setCarColor(String carColor) {
        this.carColor = carColor;
    }

    public String getCarBrand() {
        return carBrand;
    }

    public void setCarBrand(String carBrand) {
        this.carBrand = carBrand;
    }

    public String getCarLicensePlates() {
        return carLicensePlates;
    }

    public void setCarLicensePlates(String carLicensePlates) {
        this.carLicensePlates = carLicensePlates;
    }

    public String getCarDescription() {
        return carDescription;
    }

    public void setCarDescription(String carDescription) {
        this.carDescription = carDescription;
    }

    public String getCarSeat() {
        return carSeat;
    }

    public void setCarSeat(String carSeat) {
        this.carSeat = carSeat;
    }

    public long getCarPrice() {
        return carPrice;
    }

    public void setCarPrice(long carPrice) {
        this.carPrice = carPrice;
    }

    public int getCarStatus() {
        return carStatus;
    }

    public void setCarStatus(int carStatus) {
        this.carStatus = carStatus;
    }
}

this is my activity code

public class CarlistActivity extends AppCompatActivity {

    ActivityCarlistBinding binding;

    private FirebaseFirestore database;
    private CollectionReference carsRef = database.collection("cars");

    private CarListAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityCarlistBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        getSupportActionBar().setTitle("Rent Car");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        FirebaseFirestore.setLoggingEnabled(true);

        setupCarList();


    }

    private void setupCarList() {
        Query query = carsRef.orderBy("carName", Query.Direction.ASCENDING);

        FirestoreRecyclerOptions<CarRentModel> options = new FirestoreRecyclerOptions.Builder<CarRentModel>()
                .setQuery(query, CarRentModel.class)
                .build();
        adapter = new CarListAdapter(options);
        binding.carList.setHasFixedSize(true);
        binding.carList.setLayoutManager(new LinearLayoutManager(this));
        binding.carList.setAdapter(adapter);
    }

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

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}
Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807

0 Answers0