我想得到我在我的活动中运行的4个微调器中所选位置的int值。一旦我有了Int值,我想根据这个值循环并写出一个文件到SDCARD,选择IE spinner1项目4,在SD卡上生成文本文件1,2,3,4。
我不确定如何获取用户在每个微调器中单击的位置的int值,但是我想在选择图像按钮时“收集”这些值,目前这是我的代码;
//used to track spinner value
public int spinner1 = 0;
public int spinner2 = 0;
public int spinner3 = 0;
public int spinner4 = 0;
etnumber1 = (Spinner) findViewById(R.id.etnumber1);
etnumber2 = (Spinner) findViewById(R.id.etnumber2);
etnumber3 = (Spinner) findViewById(R.id.etnumber3);
etnumber4 = (Spinner) findViewById(R.id.etnumber4);
etnumber1.setOnItemSelectedListener(this);
etnumber2.setOnItemSelectedListener(this);
etnumber3.setOnItemSelectedListener(this);
etnumber4.setOnItemSelectedListener(this);
}//end of oncreate
public void onClick(View v) {
if (v==imagerun) {
for (int i=1; i<spinner1; i++){
//primary callsign
Toast.makeText(getBaseContext(), String.valueOf(i),
Toast.LENGTH_SHORT).show();
String fileName = String.valueOf(i) +".txt";
File myFile = new File("/sdcard/.working/" +fileName);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile, false);
OutputStreamWriter myOutWriter =
new OutputStreamWriter(fOut);
myOutWriter.append(etpri1.getText());
myOutWriter.append(" ");
myOutWriter.append(String.valueOf(i));
myOutWriter.close();
fOut.close();
}
}
catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(),
Toast.LENGTH_SHORT).show();
}
}//end onclick
public void onItemSelected(AdapterView<?> parent, View view1, int pos,
long id) {
// TODO Auto-generated method stub
spinner1=etnumber1.getSelectedItemPosition(); // primary number
spinner2=etnumber2.getSelectedItemPosition(); //secondary
spinner3=etnumber3.getSelectedItemPosition(); //primary number
spinner4=etnumber4.getSelectedItemPosition(); //secondary number
}
我显然做错了,我试图遵循许多教程,但它们都是指从单个微调器获取字符串值。对不起,这可能是一个简单的请求,但我一直在反对这一天!
感谢您的帮助。
答案 0 :(得分:5)
要在微调器中选择索引,请使用以下代码(以etnumber1为例)
int spinnerValue1 = etnumber1.getSelectedItemPosition();
只需复制其他微调器的代码。
答案 1 :(得分:0)
也许这就是它
public void onItemSelected(AdapterView<?> parent, View view1, int pos,
long id) {
// TODO Auto-generated method stub
switch(parent.getId()){
case R.id.etnumber1:
{
spinner1 = pos;
break;
}
case R.id.etnumber2:
{
spinner2 = pos;
break;
}
case R.id.etnumber3:
{
spinner3 = pos;
break;
}
case R.id.etnumber4:
{
spinner4 = pos;
break;
}
}}