H.Philips H.Philips - 4 months ago 16
Android Question

Changing textview using 2 spinners

I have 2 spinners and 1 textview.When i selected two different elements from these spinners textview must be changed everytime.For 1 spinner everything is OK but it doesnt work on 2 spinners.
Here's what i tried:

public class Chords extends Activity implements AdapterView.OnItemSelectedListener {
String[] notes = {"A", "B", "C", "D", "E", "F", "G"};
String[] mode = {"m","m7","5","M","7M","dim","aug","sus2"};
Spinner sp;
Spinner spinner2;
TextView textView;

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.chords_screen);
System.out.println(notes.length);
textView = (TextView) findViewById(R.id.textView);
sp= (Spinner) findViewById(R.id.spinner);
spinner2=(Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,notes);
ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,mode);
adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp.setAdapter(adapter_state);
sp.setOnItemSelectedListener(this);
spinner2.setAdapter(adapter_state2);
sp.setOnItemSelectedListener(this);

}

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

switch(position)
{

case 0:
textView.setText("Am");
break;

}


}

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

}

}

Answer

You have 2 spinners. You set onItemSelected listener. but your implementation is wrong i think. In switch case you have to identify which spinner value is changing. you can do like this ..

I edited all code for you

public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
String[] notes = {"A", "B", "C", "D", "E", "F", "G"};
String[] mode = {"m","m7","5","M","7M","dim","aug","sus2"};
Spinner sp;
Spinner spinner2;
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initialization
    textView = (TextView) findViewById(R.id.textView);
    sp = (Spinner) findViewById(R.id.spinner);
    spinner2=(Spinner) findViewById(R.id.spinner2);

    //creating adapter
    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,notes);
    ArrayAdapter<String> adapter_state2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mode);

    //setting drop down list type 
    adapter_state.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    adapter_state2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    //setting adapter to spinner
    sp.setAdapter(adapter_state);
    spinner2.setAdapter(adapter_state2);


    //setting listener to spinner
    sp.setOnItemSelectedListener(this);
    spinner2.setOnItemSelectedListener(this);
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
    // TODO Auto-generated method stub

    switch(parent.getId())
    {
        case R.id.spinner:
            textView.setText(""+notes[position]);
            break;
        case R.id.spinner2:
            textView.setText(""+mode[position]);
            break;

    }
}


@Override
public void onNothingSelected(AdapterView<?> parent) {
    // TODO Auto-generated method stub

}

To do so edit like this

    case R.id.spinner:
        textView.setText(sp.getSelectedItem().toString()+spinner2.getSelectedItem().toString());
        break;
    case R.id.spinner2:
        textView.setText(sp.getSelectedItem().toString()+spinner2.getSelectedItem().toString());
        break;
Comments