如何从setOnItemSelectedListener方法获取String?

时间:2016-03-21 12:39:10

标签: java android textview android-spinner android-button

我是Android的新手,我正试图从微调器中获取一个String。我做了一些研究,但是找不到任何有用的东西。无需尝试获取String,代码工作正常。这是正在运行的代码:

assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();

        }

        public String getSpin(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

我尝试使用getter方法,如下所示,我没有收到任何错误:

assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();

        }

        public String getSpinnerString(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

我正在尝试使用以下方法将此String设置为文本视图:

assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();

        }

        public String getSpinnerString(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

    MainActivityClassName AnObject = new MainActivityClassName();
    text.setText(AnObject.getSpinnerString());

我收到错误消息“无法解析方法getSpin()”。我确实意识到我没有将任何参数传递给方法,但我不知道我怎么能在这里做。我感谢任何帮助或任何其他建议,以不同的方式解决问题。提前谢谢。

如果有帮助,这是完整的代码:

public class MainActivity extends AppCompatActivity {

TextView text;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_json);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    text = (TextView) findViewById(R.id.textView);

    // spinner 1
    final Spinner staticSpinner1 = (Spinner) findViewById(R.id.spinner1);

    // Create an ArrayAdapter using the string array and a default spinner
    ArrayAdapter<CharSequence> staticAdapter1 = ArrayAdapter
            .createFromResource(this, R.array.lang,
                    android.R.layout.simple_spinner_item);

    // Specify the layout to use when the list of choices appears
    staticAdapter1
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // Apply the adapter to the spinner
    staticSpinner1.setAdapter(staticAdapter1);



    // spinner 2
    final Spinner staticSpinner2 = (Spinner) findViewById(R.id.spinner2);

    staticSpinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1,
                                   int arg2, long arg3) {
            String spinnerLanguage = staticSpinner1.getSelectedItem().toString();


            switch (spinnerLanguage) {
                case "Afrikaans":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterAF = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.af,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterAF
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterAF);
                    break;
                case "French":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterFR = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.fr,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterFR
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterFR);
                    break;
                case "English":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterEN = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.en,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterEN
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterEN);
                    break;
                case "Turkish":
                    // Create an ArrayAdapter using the string array and a default spinner
                    ArrayAdapter<CharSequence> staticAdapterTR = ArrayAdapter
                            .createFromResource(getBaseContext(), R.array.tr,
                                    android.R.layout.simple_spinner_item);

                    // Specify the layout to use when the list of choices appears
                    staticAdapterTR
                            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

                    // Apply the adapter to the spinner
                    staticSpinner2.setAdapter(staticAdapterTR);
                    break;
            }

        }

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

        }

    });


    assert staticSpinner2 != null;
    staticSpinner2.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

            String spinnerLanguage2 = staticSpinner2.getSelectedItem().toString();


        }

        public String getSpin(String spinnerLanguage2) {
            return spinnerLanguage2;
        }

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

        }
    });

    MainActivity AnObject = new MainActivity();
    text.setText(AnObject.getSpinnerString());

}

}

3 个答案:

答案 0 :(得分:4)

你可以这样做:

String selectedText = null;

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

        selectedText = parent.getItemAtPosition(position).toString();
    }

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

    }
});

现在,您可以使用selectedText变量传递给您想要的任何方法。但是,请记住,除非您在微调器中选择某些内容,否则它的值将为null

答案 1 :(得分:3)

如果你真的必须在外面使用它,你可以这样做:

 public void setTextView(String text){
        text.setText(text);
 }

并在onItemSelected中调用此方法:

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     setTextView(staticSpinner2.getSelectedItem().toString());
}

答案 2 :(得分:2)

读完这段代码之后,为什么不提取&#34; spinnerLanguage2&#34;到你的主类,然后从那里访问它?在OnItemSelected方法中分配值。

编辑: 我之前的回答是错的。尝试实现一个接口,以便与其他类进行通信。因此,调用该接口的方法可以传递该值。我给你一个链接(它解释了它的碎片,但你也可以传达你的类):

http://developer.android.com/intl/es/training/basics/fragments/communicating.html