Android微调器位置未更新

时间:2014-02-03 23:32:13

标签: java android

我有一个用字符串数组填充的微调器。按下按钮时会填充微调器。即使我使用微调器并选择说game_3,输出也始终是game_0。这是因为它在一个函数中吗?

    public void game(View view) throws IOException {    
    myText="";
    TextView myTextView= (TextView)findViewById(R.id.textView1);
    String game_list[] = {"game_0","game_2","game_3","game_3","game_4","game_5"};
    for(int i=0; i<game_list.length; i++){
    myText = myText + game_list[i] +"\n";
    }
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_item, game_list); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    spinner.setAdapter(adapter);
    String text = spinner.getSelectedItem().toString();
    myTextView.setText("");
    myTextView.append(text);
   }

1 个答案:

答案 0 :(得分:1)

您何时拨打game(View v)方法?微调器初始化spinner.setAdapter(adapter);和提取所选值的行之间没有用户操作的空间,显然(默认情况下)是第一个数组...“game_0”。

您可以使用OnItemSelectedListener监听微调器上的用户操作,或使用带有单击侦听器的按钮在提交时检查微调器值。

以下是应用于微调器的选定侦听器的示例:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {


        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
            // here get the selected value and do whatever you want
    }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // ignore this if you're not going to provide a "no-selection" option
        }
    });