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

时间:2023-01-21 14:52:19

I have 2 spinner. This part of code read the selection from spinner and write to text file. The problem always write the default selection and never change although if I select different selection on spinner.Please help.

我有2个旋转器。这部分代码从微调器读取选择并写入文本文件。问题始终写入默认选择并且永远不会改变,尽管我在spinner上选择了不同的选择。请帮助。

    package com.apps.androidapps10;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;

import android.os.Environment;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.EditText;
import android.view.View;
import android.widget.Spinner;
import android.widget.Toast;


public class Main2Activity extends AppCompatActivity {

    Button hantarBtn;
    private static final String TAG = "MEDIA";
    String namaJalan = "";
    String namaLorong = "";
    String noKenderaan;
    private String filename = "SampleFile.txt";
    private String filepath = "MyFileStroage";
    File myExternalFile;
    EditText inputText;
    String separator = System.getProperty("line.separator");
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        setContentView(R.layout.activity_main2);

        hantarBtn = (Button) findViewById(R.id.button);
        inputText = (EditText) findViewById(R.id.editText);

        try {

            BufferedReader bReader =  new BufferedReader(new InputStreamReader
                    (getAssets().open("jalan.txt")));
            ArrayList<String> jalan = new ArrayList<String>();
            String line = bReader.readLine();
            while (line != null){
                jalan.add(line);
                line = bReader.readLine();

            }

            bReader.close();

            Spinner spinner1 = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter<String> adapter1 =  new ArrayAdapter<String>(this,
                    android.R.layout.simple_spinner_dropdown_item,jalan);
            spinner1.setAdapter(adapter1);

            namaJalan = spinner1.getSelectedItem().toString();




        }catch (IOException e){

            e.printStackTrace();
        }

        try {
            BufferedReader bReader1 =  new BufferedReader(new InputStreamReader
                    (getAssets().open("lorong.txt")));
            ArrayList<String> lorong = new ArrayList<String>();
            String line = bReader1.readLine();
            while (line != null){
                lorong.add(line);
                line = bReader1.readLine();

            }

            bReader1.close();

            Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
            ArrayAdapter<String> adapter2 =  new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1,lorong);
            spinner2.setAdapter(adapter2);

            namaLorong = spinner2.getSelectedItem().toString();

        }catch (IOException e) {
            e.printStackTrace();
        }






        hantarBtn.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                try {

                    FileOutputStream fos = new FileOutputStream(myExternalFile,true);
                    fos.write(inputText.getText().toString().getBytes());
                    fos.write("\t".getBytes());
                    fos.write(namaJalan.toString().getBytes());
                    fos.write("\t".getBytes());
                    fos.write(namaLorong.toString().getBytes());
                    fos.write("\r\n".getBytes());
                    fos.flush();
                    fos.close();


                    Toast.makeText(getApplicationContext(), "Write Done...",
                            Toast.LENGTH_SHORT).show();
                    //Intent intent = new Intent(Main2Activity.this, Main2Activity.class);
                    //startActivity(intent);
                } catch (Exception e) {
                    e.printStackTrace();
                    Log.i(TAG, "Failed to write");
                }

            }
        });

        if (!isExternalStorageAvailable() || isExternalStorageReadOnly()) {
            hantarBtn.setEnabled(false);
        } else {
            myExternalFile = new File(getExternalFilesDir(filepath), filename);
        }
    }

    private static boolean isExternalStorageReadOnly() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(extStorageState)) {
            return true;
        }
        return false;
    }

    private static boolean isExternalStorageAvailable() {
        String extStorageState = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(extStorageState)) {
            return true;
        }
        return false;
    }



}

2 个解决方案

#1


0  

You need to move your spinner variables to the top (The same place you have Button hantarBtn;), then move your code to get the values from the spinners into the onClickListener:

您需要将微调器变量移动到顶部(与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
            }
        }
    }

#2


0  

  1. Please use resource for handling file operation (reading / writing).

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

  2. Separate the write to file to another function, then call it later

    将写入文件与另一个函数分开,然后再调用它

  3. The spinner use AdapterView will use the setOnItemSelectedListener() function, not setOnClickListener() of View

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

Use it as below

使用如下

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  

You need to move your spinner variables to the top (The same place you have Button hantarBtn;), then move your code to get the values from the spinners into the onClickListener:

您需要将微调器变量移动到顶部(与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
            }
        }
    }

#2


0  

  1. Please use resource for handling file operation (reading / writing).

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

  2. Separate the write to file to another function, then call it later

    将写入文件与另一个函数分开,然后再调用它

  3. The spinner use AdapterView will use the setOnItemSelectedListener() function, not setOnClickListener() of View

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

Use it as below

使用如下

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