使用2个微调器更改textview

时间:2016-07-10 10:38:48

标签: android android-studio textview spinner

我有2个微调器和1个textview。当我从这些微调器中选择两个不同的元素时,textview必须每次都更改。对于1个微调器,一切都可以,但它不适用于2个微调器。 这是我试过的:

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

        }

    }

1 个答案:

答案 0 :(得分:1)

你有2个微调器。您设置onItemSelected侦听器。但我认为你的实施是错误的。在切换情况下,您必须确定哪个微调器值正在改变。你可以这样做..

我为你编辑了所有代码

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

}

这样做就像这样编辑

    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;