如果我在微调器上选择不同的选择,总是写默认选择并且永远不会改变

时间:2016-11-10 04:13:34

标签: android android-studio spinner

我有2个旋转器。这部分代码从微调器读取选择并写入文本文件。问题总是写出默认选择,但是如果我在spinner上选择不同的选择,则永远不会改变。请帮助。

function download(auth, fileId, doneCb) {
  var respData = '';
  var service = google.drive('v3');

  service.files.get({
    auth: auth,
    fileId: fileId,
    alt: 'media'
  })
  .on('data', function(chunk) {
    respData += chunk;
  })
  .on('end', function () {
    // respData should contain all of the data now
    doneCb(respData);
  })
  .on('error', function (err) {
    console.log('Error during download', err);
  });
}

2 个答案:

答案 0 :(得分:0)

  1. 请使用资源处理文件操作(读/写)。

  2. 将写入文件分离到另一个函数,然后再调用

  3. 微调器使用AdapterView将使用setOnItemSelectedListener()函数,而不是View

  4. 的setOnClickListener()

    使用如下

    spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
       @Override
       public void onItemSelected(AdapterView<?> parentView, View selectedItemView, 
       int position, long id) {
        // write to file
    }
    
       @Override
       public void onNothingSelected(AdapterView<?> parentView) {
        // your code here
       }
    });
    

答案 1 :(得分:0)

您需要将微调器变量移到顶部(与Button hantarBtn;相同的位置),然后移动代码以将微调器中的值转换为onClickListener

public class MainActivity{
    Spinner spinner1;  // There's another way of doing this,
    Spinner spinner2;  // but putting them here should be easiest
    // The rest of your member variables

    @Override
    public void onCreate(){
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);

        // the rest of your code

        button.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                // Make sure to get the values when the button is clicked
                selectedItem1 = spinner1.getSelectedItem().toString();
                selectedItem2 = spinner2.getSelectedItem().toString();

                // whatever else you want to do when the button is clicked
            }
        }
    }