如何在android中显示从微调器获得的int值

时间:2011-12-09 05:43:35

标签: int settext android-spinner

我正在尝试将我得到的微调器值(基于位置)转换为int值,然后我可以在微调器下面显示。当我运行代码时,当我包含“tv.setText(position);”时会出错。但我无法弄清楚为什么。我正在尝试将TextView tv的文本设置为微调器的位置。

我做错了什么?

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemSelectedListener; 
import android.widget.ArrayAdapter; 
import android.widget.Spinner; 
import android.widget.TextView;  
public class TestThings extends Activity {      
Spinner spinner1;   
int yourInt;    
TextView tv;    
@Override   
public void onCreate(Bundle savedInstanceState) {       
super.onCreate(savedInstanceState);         
setContentView(R.layout.testing);       
spinner1 = (Spinner) findViewById(R.id.testspinner1);                       
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(              this, R.array.chpugsp1_array,               android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){                
public void onItemSelected(AdapterView<?> arg0, View arg1,                  int arg2, long arg3) {
int position  = spinner1.getSelectedItemPosition();
tv = (TextView) findViewById(R.id.testext);
tv.setText(position);
}           
@Override            
public void onNothingSelected(AdapterView<?> arg0) {                
// TODO Auto-generated method stub                          }                   
});     
}  
}

1 个答案:

答案 0 :(得分:1)

你可以试试这个:

tv.setText(""+ position);