我想得到在我的活动中运行的4个微调器中所选位置的int值。一旦我有了这个int值,我想根据这个值循环并将一个文件写入sdcard,即选择spinner1项4,在sdcard上生成文本文件1、2、3、4。
我不确定如何获取用户在每个微调器中单击的位置的int值,但我想在选择imagebutton时“收集”这些值,目前这是我的代码;

    //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
        }

很明显我做错了,我试过遵循很多教程,但它们都是指从单个微调器获取字符串值。抱歉,这可能是一个简单的要求,但我已经撞了一整天我的头!
谢谢你的帮助。

最佳答案

也许就是这样

    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;
    }

    }}

关于android - 获取从Spinner中选择的位置的Int值-Android,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15100618/

10-10 22:46