2

我想获得在我的活动中运行的 4 个微调器中选择的位置的 int 值。一旦我有了 Int 值,我想根据这个值循环并将一个文件写入 SDCARD,选择 IE spinner1 item 4,在 sdcard 上生成文本文件 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
        }

我显然做错了,我尝试遵循很多教程,但它们都指的是从单个微调器获取字符串值。抱歉,这可能是一个简单的请求,但我整天都在为此努力!

谢谢你的帮助。

4

2 回答 2

5

要在微调器中选择索引,请使用以下代码(以 etnumber1 为例)

    int spinnerValue1 = etnumber1.getSelectedItemPosition();

并为其他微调器复制代码。

于 2013-02-26T23:17:21.593 回答
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;
    }

    }}
于 2013-02-27T00:07:06.537 回答