无法使用数据库中的Spinner.setSelection

时间:2017-07-26 02:42:20

标签: android android-spinner

我使应用程序可以在用户打开活动微调器从数据库中检索数据时将微调器中的数据输入数据库,然后如果用户更改微调器的值,则刷新微调器(在我的情况下如果用户单击编辑按钮,则数据不匹配)与数据库)我该如何解决这个问题?

这是我的代码:

    protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.editfpp);
            getAlamatBill(id);
    }

    @Override
        protected void onResume() {
            super.onResume();
      spinnerprovinsibill.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String kodeprovinsi = helper.getRefprovinsi(spinnerprovinsibill.getSelectedItem().toString());
                    ambilKabupaten(kodeprovinsi, spinnerkabupatenbill);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

            spinnerkabupatenbill.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    String kodekabupaten = helper.getRefkabupaten(spinnerkabupatenbill.getSelectedItem().toString());
                    ambilKecamatan(kodekabupaten, spinnerkecamatanbill);
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });

    }

        private void ambilKecamatan(String kodekabupaten, Spinner spinnerkecamatanbill) {
            List<String> data = helper.getKecamatan(kodekabupaten);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_background, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerkecamatanbill.setAdapter(adapter);
        }

        private void ambilKabupaten(String kodeprovinsibill, Spinner spinnerkabupatenbill) {
            List<String> data = helper.getKabupaten(kodeprovinsibill);
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.spinner_background, data);
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            spinnerkabupatenbill.setAdapter(adapter);
        }

   private void ambilProvinsiPilihan(Spinner spinner, String namaprovinsi, String kabupaten, String kecamatan, String kelurahan){
        List<String> data = helper.getProvinsi();
        ArrayAdapter<String> dataadapter = new ArrayAdapter<String>(this, R.layout.spinner_background, data);
        dataadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinner.setAdapter(dataadapter);
        int posisi = dataadapter.getPosition(namaprovinsi);
        spinner.setSelection(posisi);
        System.out.println("hasil = " + helper.getRefkabupaten(kabupaten) + " " + helper.getRefkecamatan(kecamatan));

    }

private void getAlamatBill(String id) {
        try {
            System.out.println("persiapan data: " + id);
            JSONArray array = new JSONArray(helper.getAlamatShip(id));
            JSONObject object = array.getJSONObject(0);
            String alamat = object.getString("alamat");
            String kdpos = object.getString("kdpos");
            String kelurahan = object.getString("kelurahan");
            String kecamatan = object.getString("kecamatan");
            String kabupaten = object.getString("kabupaten");
            String provinsi = object.getString("provinsi");
            String kota = object.getString("kota");
            ambilProvinsiPilihanShip(spinnerprovinsiship, provinsi, kabupaten, kecamatan, kelurahan);
            ambilKotaPilihan(spinnerkotaship, kota);
            edalamatship.setText(alamat);
            edkodeposship.setText(kdpos);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

提前致谢

0 个答案:

没有答案