1

I have 2 spinner elements and I want to start an Async task only if the user changes the selection of any spinner.

The problem is, when I start the activity on my device, both Async tasks are started immediately, even the user did not select anything.

What I tried:

        final Spinner sp1 = findViewById(R.id.spinner1);
    final Spinner sp2 = findViewById(R.id.spinner2);

    final List<String> list = new ArrayList<String>();
    list.add(getString(R.string.text1));
    list.add(getString(R.string.text2));
    list.add(getString(R.string.text3));

    final List<String> list2 = new ArrayList<String>();
    list2.add(getString(R.string.item1));
    list2.add(getString(R.string.item2));
    list2.add(getString(R.string.item3));

    ArrayAdapter<String> adp1 = new ArrayAdapter<String>(this,
            R.layout.spinner_item, list);
    adp1.setDropDownViewResource(R.layout.my_spinnerlist);
    sp1.setAdapter(adp1);
    sp1.setSelection(((ArrayAdapter)sp1.getAdapter()).getPosition(default1));

    ArrayAdapter<String> adp2 = new ArrayAdapter<String>(this,
            R.layout.spinner_item, list2);
    adp2.setDropDownViewResource(R.layout.my_spinnerlist);
    sp2.setAdapter(adp2);
   sp2.setSelection(((ArrayAdapter)sp2.getAdapter()).getPosition(default2));

    sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

            String item1 = list.get(position);

            actorsList = new ArrayList<>();
            adapter = new ActorAdapter(Zoznam.this, "Zoznam", R.layout.novinkydata_item, actorsList);
            lv.setAdapter(null);lv.setAdapter(adapter); adapter.notifyDataSetChanged();
            lv.setVisibility(VISIBLE);
            new GetContacts(Zoznam.this).execute(item1,"");


        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

    sp2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
    {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) {

       String item2 = list2.get(position);

               actorsList = new ArrayList<>();
            adapter = new ActorAdapter(Zoznam.this, "Zoznam", R.layout.novinkydata_item, actorsList);
            lv.setAdapter(null);lv.setAdapter(adapter); adapter.notifyDataSetChanged();
            lv.setVisibility(VISIBLE);
            new GetContacts(Zoznam.this).execute("",item2);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub
        }
    });

So why the new GetContacts(Zoznam.this).execute... is started immediately for both listeners after the activity starts? I thought the setOnItemSelectedListener should start only if the user interacts with the selection, but not by default.

Do I have something wrong here?

PS: When then I interact with the 1st spinner, then it works fine, only 1 AsyncTask is launched, the same if I interact with the 2nd spinner - only the second AsyncTask is launched.

The issue is only when I start the activity, then both AsyncTask are launched without any interaction with spinners.

Darksymphony
  • 2,155
  • 30
  • 54
  • 2
    Calling `setOnItemSelectedListener` will cause `onItemSelected` to be called. See this SO question: https://stackoverflow.com/questions/13397933/android-spinner-avoid-onitemselected-calls-during-initialization – Jim Rhodes Aug 21 '19 at 21:06
  • Also this https://stackoverflow.com/questions/27193962/android-spinner-onitemselected-only-on-user-interaction – Mostafa Gazar Aug 21 '19 at 21:54

2 Answers2

1

The listener is triggered when you set the listener, because it triggers onItemSelected.

barq
  • 3,681
  • 4
  • 26
  • 39
  • seems so, but it doesn't help me to solve it. The link from @Jim Rhodes will probably help. I will try to use an integer value check – Darksymphony Aug 22 '19 at 08:21
  • I read your question as if you were asking for an explanation, why the behavior is as it is. Therefore I did not post any ways to avoid the behavior. – barq Aug 22 '19 at 20:28
0

finally I found an easy solution from the thread that @Jim Rhodes suggested.

  sp1.setAdapter(adp1);
  sp1.setSelected(false);
  sp1.setSelection(0,true);

After setAdapter I used false for setSelected and then OnItemSelected() is not called during initialization of spinner.

Very easy solution and working fine.

Darksymphony
  • 2,155
  • 30
  • 54