0

as demonstrated here by jalopaba, I already created a new class: How do you get the selected value of a Spinner?

public class MyItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
        String selected = parent.getItemAtPosition(pos).toString();
    }

    public void onNothingSelected(AdapterView parent) {
        // Do nothing.
    }
}

and registering this to the spinner in the original class:

spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());

However, I still can't use that selected string yet to fill in my code in the same class:

textView.setText(selected);

I'm new to this Android anyway, so this question may be too dummy to some of you

Community
  • 1
  • 1
Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
  • Initalize the string variable out side the class . Initialize it in main class above onCreate() Method. – Chirag Aug 01 '12 at 05:23

4 Answers4

2

Add the setText code in the onItemSelected:

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    yourTextView.setText(parent.getSelectedItem().toString);
}
Basant Singh
  • 5,736
  • 2
  • 28
  • 49
0

Try this code,I hope It's help you.

final CharSequence[] array_min = {"No 

  Min","100","200","300","400", "500","600","700","800","900","1000",
                  "1100","1200","1300","1400","1500","1600","1700","1800","1900","2000","2500","3000","3500" };

        Spinner s = (Spinner) layout.findViewById(R.id.viewSpin);

        adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, array_min);

        s.setAdapter(adapter);
        s.setOnItemSelectedListener(new OnItemSelectedListener() {

            public void onItemSelected(AdapterView<?> parent, View arg1,
                    int arg2, long arg3)
            {
                selItem = parent.getSelectedItem().toString();
            }

            public void onNothingSelected(AdapterView<?> arg0) {
                // TODO Auto-generated method stub
            }
        });
skt
  • 102
  • 8
0

Use global variable..make following changes in your code

String selected="";

public class MyItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
    selected = parent.getItemAtPosition(pos).toString();
}

public void onNothingSelected(AdapterView parent) {
    // Do nothing.
}

}

textView.setText(selected);

Rekha
  • 901
  • 1
  • 11
  • 20
0

After register spinner ,you can get selected item from getSelectedItem() method on any action such ocClick()

 regType = (Spinner) findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.regType, android.R.layout.simple_spinner_dropdown_item);
    regType.setAdapter(adapter);

public void onClick(View v) { switch (v.getId()) { case R.id.btnSave:

        intent.putExtra("regtype",regType.getSelectedItem().toString());
        startActivity(intent);  

            break;}}
Mohammod Hossain
  • 4,134
  • 2
  • 26
  • 37