Spinner Values is not being selected

2019-09-05 01:39发布

问题:

I have very simple spinnser in which i am showing two value 1=> English 2=> Hebrew

and i restart the whole activity (To change UI) on selecting any value from the spinner but the problem is my activity is only restarting for case 1 only please help me to figure out the problem.

Here is the code that i am using

languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                if (!isFistLaunch) {

                    String email = mEmailEditText.getText().toString();
                    String pass = mPasswordEditText.getText().toString();
                    Intent intent = new Intent(MainActivity.this, MainActivity.class);
                    intent.putExtra("typed_email", email);
                    intent.putExtra("typed_pass", pass);
                    mUserSession.setUserLanguage(lang[position]);
                    Toast.makeText(MainActivity.this, "Spinner position = " + position, Toast.LENGTH_SHORT).show();
                    startActivity(intent);
                    MainActivity.this.finish();

                } else {
                    isFistLaunch = false;
                }
            }

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

            }
        });

I also put a toast in side it but it only shows for one time...

Spinner working exactly as i want but only on my device. all of the other devices doesn't show any toast for Hebrew language. They only show Toast for English language.

can anybody tell me what is the problem here? Thanks

回答1:

try this

ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, states);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spTimeName.setAdapter(dataAdapter);

if (!compareValue.equals(null)) {
    int spinnerPosition = dataAdapter.getPosition(compareValue);
    spTimeName.setSelection(spinnerPosition);
}


回答2:

Problem is when activity load which gets first position of array is lang1

   Spinner languageSpinner = (Spinner) findViewById(R.id.spinner);

    String lang[] = {"Select lang","lang1", "lang2"};
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(JobCardActivity.this, android.R.layout.simple_list_item_1, lang);
    languageSpinner.setAdapter(adapter);

            languageSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

                        if(position != 0 || (!lang[position].equals("Select lang"))){

                        Toast.makeText(JobCardActivity.this, "Spinner position = " + position, Toast.LENGTH_SHORT).show();
                    }
                }

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

                }
            });


回答3:

In order to switch language for your app, I suggest you refer to my following code:

    protected static void setLocale(Context context, String language) {
        // if not exist, set EN as default
        Locale locale = new Locale(language);
        final Locale[] availableLocales=Locale.getAvailableLocales();
        if (!(Arrays.asList(availableLocales).contains(locale))) {
            locale = new Locale("en");
        }
        Locale.setDefault(locale);
        Configuration config = new Configuration();
        config.locale = locale;
        ((Activity) context).getBaseContext().getResources().updateConfiguration(config,
                ((Activity) context).getBaseContext().getResources().getDisplayMetrics());

        // refresh activity to reload resources
        Intent refresh = ((Activity) context).getIntent();
        ((Activity) context).overridePendingTransition(0, 0);
        refresh.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        ((Activity) context).finish();
        ((Activity) context).overridePendingTransition(0, 0);
        context.startActivity(refresh);
    }

    protected static void switchLocale(Context context) {
        Locale current = context.getResources().getConfiguration().locale;
        if (current.getLanguage().equals("he")){
            setLocale(context, "en");
        } else {
            setLocale(context, "he");
        }
    }

Then, inside onClick of Button or onItemSelected of Spinner..., for example, you can call switchLocale(mContext);

P/S: English resources will be stored in \app\src\main\res\values, Hebrew resources will be stored in \app\src\main\res\values-he

You can read more at Google's Doc - Supporting Different Languages.

Hope this helps!