Android:spinners& setOnItemSelectedListener& getSharedPreferencesString

时间:2016-03-16 17:15:16

标签: android spinner android-spinner

1)在我的活动中,我有两个微调器: countrySpinner citySpinner 。第二个微调器依赖于第一个 - 如果用户从countrySpinner中选择项目A,则第二个citySpinner中的值也将被更新,例如,如果用户从第一个选择俄罗斯,在第二个旋转器中他将获得俄罗斯城市......

< - 它工作。

2)在我的活动中,我有一个按钮,用于保存用户在微调器中提供的信息。信息存储在共享应用程序首选项中。我没有在代码示例中包含它,因为它不相关。

< - 它工作

3.在活动的onCreate方法中,我读取了以前保存的值 - > checkIfLocalSettingExists())并分配保存到'countryStr'和'的值。 'cityStr'变量。

< - 它工作

4.然后我想更新在开头有默认值的微调器。我有一些存储在共享首选项中的字符串,所以为了获得正确的元素我正在使用getIndex函数,它返回一个应该设置的元素的id。

< - IT WORKS

5.我得到需要在两个微调器上设置的元素后,我可以设置它们。我将检索到的id分配给微调器,但是,setOnItemSelectedListener执行两次不知道原因,可能是因为它不起作用!。

< - 它不工作!

代码:

public class Settings extends ActionBarActivity{
public Spinner countrySpinner;
public Spinner citySpinner;
public String cityStr;
public String countryStr;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat_settings);
    countrySpinner = (Spinner) findViewById(R.id.kraje_spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Countries, R.layout.spinner_layout);
    countrySpinner.setAdapter(adapter);

    citySpinner = (Spinner) findViewById(R.id.przejscie_spinner);
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesRussia, R.layout.spinner_layout);
    citySpinner.setAdapter(adapter2);


    if (checkIfLocalSettingExists())
    {           
        System.out.println("Gathered following values from settings");
        System.out.println("Country: " + countryStr);
        System.out.println("City: " + cityStr);
        System.out.println("----");
        System.out.println("Setting the spinners...");
        System.out.println("Country spinner:");
        // I found the ID of saved country, I can set it in countrySpinner
        countrySpinner.setSelection(getIndex(countrySpinner, countryStr));
        System.out.println("City spinner:");

        //without this it didn't work at all, don't know why            
        if (kraj.toLowerCase().equals("russia"))
            adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesRussia, R.layout.spinner_layout);
        else if (kraj.toLowerCase().equals("ukraine"))
            adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesUkraine, R.layout.spinner_layout);
        else {
            adapter2 = ArrayAdapter.createFromResource(this, R.array.CitiesBelarus, R.layout.spinner_layout);
        }
        citySpinner.setAdapter(adapter2);

        // I found the ID of saved city, I can set it in citySpinner
        citySpinner.setSelection(getIndex(citySpinner, cityStr));       
    }   


    countrySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {               
            if (position == 0) {
                System.out.println("SELECTED RUSSIA");
                ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Settings.this,
                        R.array.CitiesRussia, R.layout.spinner_layout);                    
                adapter2.setDropDownViewResource(R.layout.spinner_layout);
                citySpinner.setAdapter(adapter2);
            }
            //
            else if (position == 1) {
                System.out.println("SELECTED UKRAINE");
                ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Settings.this,
                        R.array.CitiesUkraine, R.layout.spinner_layout);
                adapter2.setDropDownViewResource(R.layout.spinner_layout);
                citySpinner.setAdapter(adapter2);
            } else {
                System.out.println("SELECTED BELARUS");
                ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(Settings.this,
                        R.array.CitiesRussia, R.layout.spinner_layout);
                adapter2.setDropDownViewResource(R.layout.spinner_layout);
                citySpinner.setAdapter(adapter2);
            }              
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });
   }
private int getIndex(Spinner spin, String myString){
    int index = 0;
    String tmp = "";
    System.out.println("Im in get index function!");
    System.out.println("Seraching for: " + myString);
    for (int i=0;i<spin.getCount();i++){
        tmp = spin.getItemAtPosition(i).toString();
        System.out.println("checking if " + tmp.toLowerCase() + " equals " + myString.toLowerCase());
        if (tmp.toLowerCase().equals(myString.toLowerCase())){
            System.out.println("FOUND IT --> " +tmp + " ---->" + i +", its: " + myString);
            index = i;
            break;
        }
        else
            System.out.println("NOT FOUND IT --> " +tmp + "-->" + i +", its NOT: " + myString);

    }
    return index;
}

代码输出:

03-16 15:51:12.590  I/System.out: Gathered following values from settings
03-16 15:51:12.590  I/System.out: Country: Belarus
03-16 15:51:12.590  I/System.out: City: Terespol
03-16 15:51:12.590  I/System.out: ----
03-16 15:51:12.590  I/System.out: Setting the spinners...
03-16 15:51:12.590  I/System.out: Country spinner:
03-16 15:51:12.590  I/System.out: Im in get index function!
03-16 15:51:12.590  I/System.out: Seraching for: Belarus
03-16 15:51:12.590  I/System.out: checking if russia equals belarus
03-16 15:51:12.590  I/System.out: NOT FOUND IT --> russia -->0, its NOT: Belarus
03-16 15:51:12.590  I/System.out: checking if ukraine equals belarus
03-16 15:51:12.590  I/System.out: NOT FOUND IT --> ukraine-->1, its NOT: Belarus
03-16 15:51:12.590  I/System.out: checking if belarus equals belarus
03-16 15:51:12.590  I/System.out: FOUND IT --> Belarus ---->2, its: Belarus
03-16 15:51:12.590  I/System.out: City spinner:
03-16 15:51:12.590  I/System.out: Im in get index function!
03-16 15:51:12.590  I/System.out: Seraching for: Terespol
03-16 15:51:12.590  I/System.out: checking if cityA equals terespol
03-16 15:51:12.590  I/System.out: NOT FOUND IT --> cityA -->0, its NOT: Terespol
03-16 15:51:12.590  I/System.out: checking if cityB equals terespol
03-16 15:51:12.590  I/System.out: NOT FOUND IT --> cityB -->1, its NOT: Terespol
03-16 15:51:12.590  I/System.out: checking if cityC equals terespol
03-16 15:51:12.594  I/System.out: NOT FOUND IT --> cityC -->2, its NOT: Terespol
03-16 15:51:12.594  I/System.out: checking if cityD equals terespol
03-16 15:51:12.594  I/System.out: NOT FOUND IT --> cityD -->3, its NOT: Terespol
03-16 15:51:12.594  I/System.out: checking if cityE equals terespol
03-16 15:51:12.594  I/System.out: NOT FOUND IT --> cityE -->4, its NOT: Terespol
03-16 15:51:12.594  I/System.out: checking if terespol equals terespol
03-16 15:51:12.594  I/System.out: FOUND IT --> Terespol ---->5, its: Terespol
03-16 15:51:12.594  I/System.out: 5
03-16 15:51:12.642  I/System.out: SELECTED BELARUS

因此白俄罗斯代码是2,它由getIndex函数正确返回,然后将微调器设置为有效值(白俄罗斯)。 Terespol的返回码是5,由getIndex函数正确返回,然后由于再次为countrySpinner执行setOnItemSelectedListener这一事实,spinner未正确设置(从输出中检查最后一行)。为什么?我只为countrySpinner设置了监听器,而不是为citySpinner设置了监听器?!因此,citySpinner不是第5个元素而是第1个元素。为什么在世界上它不起作用!?

2 个答案:

答案 0 :(得分:0)

每当您手动选择微调器中的任何项目或通过调用此类setSelection()时 -

countrySpinner.setSelection(getIndex(countrySpinner, countryStr));

它肯定会调用onItemSelected()并且在onItemSelected()中您正在加载您的城市微调器值,这会重置您的选择,尽管您之前选择了特定项目。

要解决此问题,您必须跟踪是否已从checkIfLocalSettingExists()加载了国家/地区微调器值,在这种情况下,您不必设置城市微调器适配器onItemSelected(),因为你已经这样做了。

答案 1 :(得分:0)

我设法通过设置额外的监听器来实现这一点,其中我将allowModification标志设置为true。仍然不知道为什么以前它不起作用以及为什么监听器总是最终被执行(bug?)

 citySpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long arg3) {
            allowModification = true;

        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });