Вопрос

I am using the same spinner to all the activities in my app. Let's say, items in spinner are English, Finnish, Polish, blah.. When I select Finnish in one activity, the dropdown of spinner gets close and Finnish can be seen on the spinner. But when I go to the next activity it does not show the Finnish anymore, instead, it shows English (item) at the top. Is there any method to make appear the item on the spinner that has been selected to all the activities. Or, should not change unless it has not been changed. In BaseActivity;

int currentLanguage = spinner.getSelectedItemPosition();
SharedMemory.getInstance().setCurrentPosition(currentLanguage);

Log.i("SET POSITION", Integer.toString(currentLanguage));

In other activity to save the state of item position;

int position = SharedMemory.getInstance().getCurrentPosition();
    Base_Activity.spinner.setSelection(position);
    Log.i("GET POSITION", Integer.toString(position));

I managed to set the selected language to set it in SharedMemory which is Singleton class. But how can I still get the same currentLanguage in all the activities. I am not able to get it when I change the activity. Note: this spinner is in action bar in Base_Activity class where all the other activities are extended this class. Thank you.

Это было полезно?

Решение

Finally, I managed to solve my problem. Before I was trying to do stuffs inside spinner.setOnItemSelectedListener() method, which was really wrong. In my BaseActivity;

Configuration conf = getResources().getConfiguration();

    //creating the position of the language in spinner from arraylist 
    int currentLanguage = Arrays.asList(language_codes).indexOf(
            conf.locale.getLanguage());

    spinner.setSelection(currentLanguage);

This allows me the selected item of spinner remain on the top always when I even change the activities. Hope this might help others. :)

Другие советы

That is because the spinner in the other activity is different from the one in the first activity.

You will have to pass the current selection of the spinner as argument to the second activity and set the other spinners selection to that position.

In BaseActivity:

    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra("pos", spinner.getSelectedItemPosition());
    startActivity(intent);

In second Activity:

    spinner.setSelection(getIntent().getIntExtra("pos", 0));

The singleton class should probably look like this:

public class SharedMemory {
    public static SharedMemory sharedMem = new SharedMemory();
    private String valueSelected; // or position here

    public SharedMemory getInstance() {
        return sharedMem;
    }

    public String getCurrentLanguage() {
        Log.i("tag","valueSelected: " + valueSelected);
        return valueSelected;
    }

    public void setCurrentLanguage(String currLang) {
        valueSelected = currLang;
        Log.i("tag","valueSelected: " + valueSelected);
    }
}

EDIT:

onItemSelectedListener(...) {
    if(ShareMemory.getInstance().getCurrentLanguage().equalsIgnoreCase("0")) {
          // then set the value to 0
    }else {
       // set the value still but then set the gui also
    }
}

// in the onResume or onCreate method write the following:

spinner.setSelection(ShareMemory.getInstance().getCurrentLanguage());

Using Log print the values in the singleTon class, and also the onItemSelected listener of the spinner.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top