无法将@override方法之外的选定微调器项获取到另一个类中

eqfvzcg8  于 2021-06-27  发布在  Java
关注(0)|答案(1)|浏览(320)

我有mainactivity和spinneractivity,我想从spinneractivity中获取选中的微调器项值到mainactivity中。我尝试将字符串声明为静态,也尝试使用getter,但没有成功。
我应该如何在@override方法之外获取值并将其放入另一个类中?获得价值的正确方法是什么?
主活动类

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;

//    public static String getInSpinnerSelectedProvince() {
//        return inSpinnerSelectedProvince;
//    }

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

        inSpinnerSelectedProvince = parent.getItemAtPosition(position).toString();

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

    }

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

    }
}

在spinner活动中,我还将使用if语句,我认为它应该是相同的,但是无论如何,我应该如何将“if语句”和“@override method”之外的值获取到另一个类中?

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) {

    }
}
nkoocmlb

nkoocmlb1#

我正要建议您使用intent在两个活动之间共享数据,这时我注意到您的“spinneractivity”实际上不是一个活动,因为它不扩展android活动类(appcompactactivity或类似的其他类)。
你的天生就是个倾听者。您可以使用它来实现在微调器视图上执行操作时要触发的操作。为此,您需要在onitemselected的“@override”方法中执行。
如果由于override方法而不喜欢使用上一个方法,则应通过执行以下操作,直接在mainactivity的微调器视图上的单击事件上实现要触发的操作:

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);
      }
   }

相关问题