1

I am developing an android application which in I have a class A. The A class have a spinner, and there is a B class which have the spinner's onItemSelected(), B implements OnItemSelectedListener and B is public. I want to call a function in A when a newer item has selected. So if there selected the first item, and I change it to the second then I want call a function from class A.
I always want to call the same A's function. It's refreshes a list.
How can I do that?
There's the class A's spinner which call in A's onCreate:

 public void addListenerOnSpinnerItemSelection() {
    pspinner = (Spinner) findViewById(R.id.spinner1);
    pspinner.setOnItemSelectedListener(new B());
}

And the B class:

public class B implements OnItemSelectedListener {
    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
        Toast.makeText(parent.getContext(),
            "The selected place: " + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
            A.refresh(); //I think I should call A's function like that but 
                         // Android Studio tells meg that refresh() must be static
    }
}

Should I call the A's function in addListenerOnSpinnerItemSelection()? Or in B's onItemSelected? But how?
Unfortunatelly it's not work that way above. How can I call A's function without make refresh() static? The spinner gets its elements from an xml.

2 Answers2

1

in case of small number of indexes you can use this

public void onClick(View v) {
        startActivity(new Intent(UserLogin.this, ResetEmail.class));
    }
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
    {
        if (parent.getSelectedItemPosition() == 0) 
        {
            someFunction();
        } 
        else if (parent.getSelectedItemPosition() == 1) 
        {
            someFunction();
        } 
        else if (parent.getSelectedItemPosition() == 2)
        {
            someFunction();
        }
    }

good luck .

-1

You could use an if/else statement or switch in your onItemSelectedListener. In your onItemSelectedListener, try this:

String s = parent.getSelectedItem().toString();

if(s == "1"){
    callFunctionHere();
}
else if(s == "2"){
    anotherFunction();
}

Inspired by this SO question

Community
  • 1
  • 1
Steve C.
  • 1,333
  • 3
  • 19
  • 50
  • Oh sorry @Steve maybe I forget to mention that how I always want to call the same function at every selected change. But how to call A's function in B? :O `a.function()` ? – Caroline Koosy Mar 18 '16 at 20:20
  • Yes you would just call a.function(); at each position – Steve C. Mar 18 '16 at 20:23
  • This isn't the correct way to [compare Strings in Java](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – codeMagic Mar 18 '16 at 20:51
  • @SteveC. that's precisely what downvotes are for (showing something that is completely wrong). If you feel it is a duplicate of that one then it should be flagged as such. The other answer was comparing `int`s not `String`s. If it helps, I waited awhile after my comment before downvoting to give you a chance to edit but you didn't. – codeMagic Mar 20 '16 at 00:54
  • Unfortunately I can't call in `B`, `A.refresh()`, it's says `refresh()` must be static. But in `refresh()` there's an other function which call's an `AsyncTask` and that cannot be static, but without that I can't call that! I don't understand in `onItemSelected()` why need be the `refresh()` static? – Caroline Koosy Mar 22 '16 at 09:44
  • @CarolineKoosy It's telling you that because when you request a method from another class it has to be static in order to access it. Otherwise it is only accessible from within its class. Are you trying to refresh network data? If so, why not use Volley? – Steve C. Mar 23 '16 at 01:48
  • @CarolineKoosy Have you seen [this](http://stackoverflow.com/questions/9185117/asynctask-inside-a-static-method-good-coding-practice)? – Steve C. Mar 23 '16 at 02:03