Search code examples
javaandroidandroid-sqlite

Can't set a default value on spinner


I want to retrieved the status value from the database and set it on a spinner. I am doing the following but I am getting java.lang.NumberFormatException: For input string: "Cancelled"

spinner.setSelection(Integer.parseInt(job.getStatus()));
   JobRepository jobRepository = new JobRepository(a.getApplication());
        VehicleRepository vehicleRepository = new VehicleRepository(a.getApplication());
        jobRepository.find(UUID.fromString(jobId)).observe(getViewLifecycleOwner(), jc -> {
            vehicleRepository.findByJob(fromString(jobId)).observe(getViewLifecycleOwner(), vehicle -> {
                Job job = jc.job;
                Customer customer = jc.customer;
                status.setText(job.getStatus());
                customerName.setText(customer.getName());
                postcode.setText(job.getPostcode());
                notesView.setEms(4);
                notesView.setMaxLines(5);
                notesView.setText(job.getNotes());
                phoneNum.setText(customer.getTel());
                emailAdd.setText(customer.getEmail());
                emailAdd.setMaxLines(2);

                spinner.setSelection(Integer.parseInt(job.getStatus()));

                if (job.getUpdatedAt() != null) {
                    lastUpdate.setText(sdf.format(job.getUpdatedAt()));
                }


Solution

  • First of all you need to ensure job.getStatus() it's an Int since setSelection(int position is waiting you to pass an Int and Cancelled can not be converted to an Int even if you use Integer.parseInt().

    Also I don't see where you set the items, so perhaps is a good idea to set items and then do the setSelection() otherwise it won't work neither.