-2

i have many of spinner (total 4) that has a list from json. and spinner after it will fill based on what i choose on the spinner before it.

this is my code to get json to spinner (example i just put 2 spinner)

 class daftarGedung extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(TambahLaporActivity.this);
            pDialog.setMessage("Mengambil Data...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            link_url = "https://example.com/api/assets?parent=0";
            HttpHandler sh = new HttpHandler();
            String json = sh.makeServiceCall(link_url);
            final boolean ambilspn = gedungSpn.getSelectedItem() == tidakada;

            Log.e(TAG, "Response from url: " + json);
            if (json != null) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject ar = data.getJSONObject(i);
                        gedung = ar.getString("nama");
                        id_gedung_int = ar.getInt("id");
                        id_gedung1 = ar.getString("id");
                        Log.d(TAG, String.valueOf(id_gedung_int));

                        list.add(gedung);
                        list_id.add(id_gedung1);

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    data_adapter_gedung.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    gedungSpn.setAdapter(data_adapter_gedung);
                    Log.e("isi gedung", "" + ambilspn);
                    if (ambilspn == false) {
                        gedungSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
//                            if (gedungSpn.getSelectedItem() != tidakada) {
                                id_gedung_int = list.indexOf(gedungSpn.getSelectedItem());
                                id_gedung = list_id.get(id_gedung_int);
                                list2.clear();
                                list2_id.clear();
                                new daftarLantai().execute();

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {
                                komen = "not_selected";
                            }
                        });
                    }

                }
            });
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
        }
    }

    class daftarLantai extends AsyncTask<String, String, String> {
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(TambahLaporActivity.this);
            pDialog.setMessage("Mengambil Data...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            link_url = "https://monasdev.example.com/api/assets?parent=" + id_gedung;
            HttpHandler sh = new HttpHandler();
            String json = sh.makeServiceCall(link_url);
            final boolean ambilspn = gedungSpn.getSelectedItem() == tidakada;
            list2.add(tidakada);

            Log.e(TAG, "Response from url: " + json);
            if (json != null) {
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    JSONArray data = jsonObject.getJSONArray("data");
                    for (int i = 0; i < data.length(); i++) {
                        JSONObject ar = data.getJSONObject(i);
                        lantai = ar.getString("nama");
                        id_lantai1 = ar.getString("id");

                        if (ambilspn == false) {
                            list2.add(lantai);
                            list2_id.add(id_lantai1);
                        } else {
                            list2.add(tidakada);
                            list3.add(tidakada);
                            list4.add(tidakada);
                        }
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {


                    data_adapter_lantai.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//                    
                    lantaiSpn.setAdapter(data_adapter_lantai);
                    data_adapter_ruang.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    ruangSpn.setAdapter(data_adapter_ruang);
                    data_adapter_aset.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    asetSpn.setAdapter(data_adapter_aset);
//                    Log.e("isi",""+ambilspn);
                    if (ambilspn == false) {
                        lantaiSpn.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                            @Override
                            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                                id_lantai_int = list2.indexOf(lantaiSpn.getSelectedItem());
                                id_lantai = list2_id.get(id_lantai_int);
                                list3.clear();
                                list3_id.clear();

                                new daftarRuangan().execute();

                            }

                            @Override
                            public void onNothingSelected(AdapterView<?> adapterView) {
                                komen = "not_selected";
                            }
                        });
//                    
                    } else {
                        list2.add(tidakada);
                        list2_id.add(id_lantai1);
                    }
                }
            });
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            pDialog.dismiss();
        }
    }

it sometimes work and sometimes not. for example it will show error java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2 when i select second list of first spinner. and it can be error java.lang.IndexOutOfBoundsException: Invalid index 5, size is 5 and so on based on what spinner that make the app crash. but the error always the same just different index(number).

this is a whole error that i get

java.lang.IndexOutOfBoundsException: Invalid index 2, size is 2
                         at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
                         at java.util.ArrayList.get(ArrayList.java:308)
                         at com.mqa.android.monas.Activity.TambahLaporActivity$daftarGedung$1$1.onItemSelected(TambahLaporActivity.java:647)
                         at android.widget.AdapterView.fireOnSelected(AdapterView.java:914)
                         at android.widget.AdapterView.dispatchOnItemSelected(AdapterView.java:903)
                         at android.widget.AdapterView.access$300(AdapterView.java:48)
                         at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:873)
                         at android.os.Handler.handleCallback(Handler.java:739)
                         at android.os.Handler.dispatchMessage(Handler.java:95)
                         at android.os.Looper.loop(Looper.java:135)
                         at android.app.ActivityThread.main(ActivityThread.java:5254)
                         at java.lang.reflect.Method.invoke(Native Method)
                         at java.lang.reflect.Method.invoke(Method.java:372)
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

please help what is wrong with my code

Mo Ne
  • 129
  • 2
  • 11

2 Answers2

0

i don't get the question but try this and tell me it work or not

 id_lantai = list2_id.get(id_lantai_int - 1);
Radesh
  • 13,084
  • 4
  • 51
  • 64
0

It looks like you aren't keeping list2 and list2_id in sync. You have a number of places where you add an item to list2 but not to list2_id, so when you run

id_lantai_int = list2.indexOf(lantaiSpn.getSelectedItem());
id_lantai = list2_id.get(id_lantai_int);

you can get an IndexOutOfBoundsException. Say you have 10 items in list2, but only 4 items in list2_id, then id_lantai_int could be anything from 0 to 9.

It would be more robust to make whatever list2 is holding have an id attribute rather than trying to keep two lists in sync, but it's not clear how that would look just based on the code you posted.

Also, to get the index of a selected item in the displayed list, you can just use third argument (int i) to onItemSelected

Tyler V
  • 9,694
  • 3
  • 26
  • 52