无法在微调器上设置默认值

Can't set a default value on spinner

我想从数据库中检索状态值并将其设置在微调器上。 我正在执行以下操作,但我得到 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()));
                }

首先你需要确保 job.getStatus() 它是一个 Int 因为 setSelection(int position 正在等待你通过 IntCancelled 不能即使您使用 Integer.parseInt().

也会转换为 Int

我也没有看到你在哪里设置项目,所以也许是设置项目然后执行 setSelection() 的好主意,否则它也不会起作用。