选择Spinner数组到TextView中

时间:2012-03-20 02:06:53

标签: java android

我的旋转器从阵列中拉出来(可行) 我想要发生的是我有几个文本视图将映射到从微调器阵列中选择的任何内容......例如不列颠哥伦比亚省。然后Texviews都会说英国哥伦比亚。 我想我应该使用某种类型的setText,但已经能够找到一个例子anyhelp将不胜感激。 这是我的微调代码:

Spinner spinner = (Spinner) findViewById(R.id.spinner);    
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(            
            this, R.array.material_array, android.R.layout.simple_spinner_item);    
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);    
    spinner.setAdapter(adapter);

    final TextView tv34=(TextView) findViewById(R.id.tv34); 
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 

        public void onItemSelected(AdapterView<?> parent, 
        View view, int pos, long id) { 
        tv34.setText(parent.getItemAtPosition(pos).toString()); 
        } 
        public void onNothingSelected(AdapterView<?> parent) { 
        // Do nothing. 
        }        
        });

1 个答案:

答案 0 :(得分:0)

试试这个:

final String[] array=getResources().getStringArray(R.array.material_array);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int position, long arg3) {

        mTextView_1.setText(array[position]);
        mTextView_2.setText(array[position]);
        mTextView_3.setText(array[position]);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {                
    }
});

编辑:

或者你可以这样做:

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1,
            int position, long arg3) {

        String selectedItem=spinner.getSelectedItem().toString();
        mTextView_1.setText(selectedItem);
        mTextView_2.setText(selectedItem);
        mTextView_3.setText(selectedItem);
    }
    @Override
    public void onNothingSelected(AdapterView<?> arg0) {                
    }
});