Darksymphony
Darksymphony

Reputation: 2693

Android spinner onselect AsyncTask

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.

Upvotes: 1

Views: 144

Answers (2)

Darksymphony
Darksymphony

Reputation: 2693

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.

Upvotes: 0

barq
barq

Reputation: 3711

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

Upvotes: 1

Related Questions