Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
220 views
in Technique[技术] by (71.8m points)

java - Can't get selected spinner item outside @Override method and into another class

I have MainActivity and SpinnerActivity, I want to get selected spinner item value from SpinnerActivity into the MainActivity. I tried to declare String as Static, I also tried with getter but without any success.

in Spinner Activity I also have if statement, how can I get value outside "if statement" and "@Override method" into another class?

MainActivity.class

public class MainActivity extends AppCompatActivity {

    SpinnerActivity spinnerActivity = new SpinnerActivity();

    Spinner spinnerProvince;

    String selectedSpinnerProvince = spinnerActivity.inSpinnerSelectedProvince;
//    String selectedSpinnerProvince = SpinnerActivity.inSpinnerSelectedProvince;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinnerProvince = findViewById(R.id.spinnerProvince);

        populateSpinnerProvinces();

        spinnerProvince.setOnItemSelectedListener(spinnerActivity);

    }

    public void populateSpinnerProvinces() {
        ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.province));
        provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProvince.setAdapter(provincesAdapter);
    }
}

SpinnerActivity.class

public class SpinnerActivity implements android.widget.AdapterView.OnItemSelectedListener {

    public String inSpinnerSelectedProvince;


    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        if (parent.getId() == R.id.spinnerProvince) {

            inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();
            Toast.makeText(parent.getContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

        } else {// code here}
        
    }

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

    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I were about to advice you to use Intent to share data between both activities when i noticed that your "SpinnerActivity" is not really an Activity since it not extends Android Activity class (AppCompactActivity or other classes like this).

Your SpinnerActivity is a Listener. You can use it to implement the action to trigger when an action is performed on your Spinner view. For that you need to do it inside the "@Override" method of onItemSelected.

If you don't like to use the previous methode because of the Override methode you should implement directly the action to trigger at on click events on your spinner view in your MainActivity by doing this:

     public class MainActivity extends AppCompatActivity {

      SpinnerActivity spinnerActivity = new SpinnerActivity();

      Spinner spinnerProvince;

       String selectedSpinnerProvince = spinnerActivity.inSpinnerSelectedProvince;
       //    String selectedSpinnerProvince = 
       SpinnerActivity.inSpinnerSelectedProvince;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        spinnerProvince = findViewById(R.id.spinnerProvince);

        populateSpinnerProvinces();
         
        //spinnerProvince.setOnItemSelectedListener(spinnerActivity);
     spinnerProvince.setOnItemSelectedListener(android.widget.AdapterView.OnItemSelectedListener() {
         @Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

    if (parent.getId() == R.id.spinnerProvince) {

        inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();
        Toast.makeText(parent.getContext(), parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show();

    } else {// code here}
    
}

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

}
    });

    }

    public void populateSpinnerProvinces() {
        ArrayAdapter<String> provincesAdapter = new ArrayAdapter<>(this, 
     android.R.layout.simple_spinner_item, 
     getResources().getStringArray(R.array.province)); 
  
 provincesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerProvince.setAdapter(provincesAdapter);
      }
   }

 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...