Features such as Spinner, saving value changes with Shared Preference,

Asked 1 years ago, Updated 1 years ago, 84 views

public class RD_OSBG : Fragment
{
    Spinner _BG_Position;

    public override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        ISharedPreferences prefs = Application.Context.GetSharedPreferences("Test", FileCreationMode.Private);
        prefs.GetString("Test_Data", "");

        // // Create your fragment here
    }

    public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View _RD_OSBG = inflater.Inflate(Resource.Layout.RD_OSBG_Layout, container, false);


        Switch _OS_Mark = (Switch)_RD_OSBG.FindViewById(Resource.Id._OS_Mark);
        _OS_Mark.CheckedChange += (s, e) =>
        {
            if (e.IsChecked == true)
            {
                Toast.MakeText (this.Activity, "Use OS Mark", ToastLength.Short).Show();
            }
            else if (e.IsChecked == false)
            {
                Toast.MakeText (this.Activity, "Do not use OS Mark", ToastLength.Short).Show();
            }
        };


        TextView _OS_Length_Text = (TextView)_RD_OSBG.FindViewById(Resource.Id._OS_Length_Text);
        SeekBar _OS_Length = (SeekBar)_RD_OSBG.FindViewById(Resource.Id._OS_Length);
        int _Setup_OS_Length_Text = 0;
        _OS_Length_Text.Text = "OS Length : " + _Setup_OS_Length_Text + "ft";
        _OS_Length.ProgressChanged += (s, e) =>
        {
            //int _Setup_Progress = e.Progress;
            _OS_Length_Text.Text = string.Format("OS Length : " + e.Progress + "ft");
        };


        TextView _OS_Beam_Text = (TextView)_RD_OSBG.FindViewById(Resource.Id._OS_Beam_Text);
        SeekBar _OS_Beam = (SeekBar)_RD_OSBG.FindViewById(Resource.Id._OS_Beam);
        int _Setup_OS_Beam_Text = 0;
        _OS_Beam_Text.Text = "OS Beam : " + _Setup_OS_Beam_Text + "ft";
        _OS_Beam.ProgressChanged += (s, e) =>
        {
            //int _Setup_Progress = e.Progress;
            _OS_Beam_Text.Text = string.Format("OS Beam : " + e.Progress + "ft");
        };


        Switch _BG_Mark = (Switch)_RD_OSBG.FindViewById(Resource.Id._BG_Mark);
        _BG_Mark.CheckedChange += (s, e) =>
        {
            if(e.IsChecked == true)
            {
                Toast.MakeText (this.Activity, "Enable the Barge Mark", ToastLength.Short).Show();
            }
            else if(e.IsChecked == false)
            {
                Toast.MakeText (this.Activity, "Disable the Barge Mark", ToastLength.Short).Show();
            }
        };


        _BG_Position = (Spinner)_RD_OSBG.FindViewById(Resource.Id._BG_Position);
        ArrayList _BG_Position_Contents = new ArrayList { "Bow", "Stern" };
        _BG_Position.Adapter = new ArrayAdapter(this.Activity, Resource.Layout.Custom_Spinner, Resource.Id._Custom_Spinner_Text, _BG_Position_Contents);

        ISharedPreferences prefs = Application.Context.GetSharedPreferences("Test", FileCreationMode.Private);
        ISharedPreferencesEditor editor = prefs.Edit();

        _BG_Position.ItemSelected += (s, e) =>
        {
            switch(e.Position)
            {
                case 1:
                    Toast.MakeText(this.Activity, "Stern", ToastLength.Short).Show();
                    editor.PutString("Test_Data", "Stern");
                    editor.Commit();
                    break;
            }

        };


        TextView _BG_Length_Text = (TextView)_RD_OSBG.FindViewById(Resource.Id._BG_Length_Text);
        SeekBar _BG_Length = (SeekBar)_RD_OSBG.FindViewById(Resource.Id._BG_Length);
        int _Setup_BG_Length_Text = 0;
        _BG_Length_Text.Text = "BG Length : " + _Setup_BG_Length_Text + "ft";
        _BG_Length.ProgressChanged += (s, e) =>
        {
            //int _Setup_Progress = e.Progress;
            _BG_Length_Text.Text = string.Format("BG Length : " + e.Progrhess + "ft");
        };


        TextView _BG_Beam_Text = (TextView)_RD_OSBG.FindViewById(Resource.Id._BG_Beam_Text);
        SeekBar _BG_Beam = (SeekBar)_RD_OSBG.FindViewById(Resource.Id._BG_Beam);
        int _Setup_BG_Beam_Text = 0;
        _BG_Beam_Text.Text = "BG Beam : " + _Setup_BG_Beam_Text + "ft";
        _BG_Beam.ProgressChanged += (s, e) =>
        {
            //int _Setup_Progress = e.Progress;
            _BG_Beam_Text.Text = string.Format("BG Beam : " + e.Progress + "ft");
        };


        return _RD_OSBG;
    }
}

It's like this right now Save the changes in the values of Spinner, Switch, and SeekBar How do I make sure that the value remains at the changed value when I enter again after exiting the fragment?

I put "Shared Preference" in the spinner part and sang it in the OnCreate" part There was an error TT

sharedpreferences

2022-09-22 14:49

2 Answers


//Get the preference value and set it to spinner
val pref = getSharedPreferences("pref", Context.MODE_PRIVATE)
spinner.setSelection(pref.getInt("spinnerSelectedPosition", 0))

//Save values in preference
spinner.setOnItemClickListener { parent, view, position, id ->
     val editor = getSharedPreferences("pref", Context.MODE_PRIVATE).edit()
     editor.putInt("spinnerSelectedPosition", position)
     editor.apply()
}

It's going to be roughly the same shape as above.

pref.getInt("spinnerSelectedPosition", 0)

where getInt() the second factor value is the default value and is automatically set when the preference does not have that value.


2022-09-22 14:49

Call setSelection after assigning adapter object to spinner. Looking at the spinner internal code, there is a logic that initializes the position after the adapter is assigned, so you should pay attention to this. I don't know where and how you can't say getSelectedItemPosition() doesn't work. I am attaching the test code that I made at kotlin.

const val KEY_LAST_SELECTED_POSITION = "last_selected_position"

class TestFragment : Fragment() {

    private var currentSelectedPosition = Spinner.INVALID_POSITION

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_content, container, false)
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)

        val adapter = ArrayAdapter.createFromResource(context, R.array.planets_array, android.R.layout.simple_spinner_item)
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        testSpinner.adapter = adapter

        currentSelectedPosition = loadLastSelectedPosition()
        testSpinner.setSelection(lastSelectedPosition)

        testSpinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                if (currentSelectedPosition != position) {
                    saveLastSelectedPosition(position)
                    currentSelectedPosition = position
                }
            }
        }
    }

    private fun loadLastSelectedPosition(): Int {
        return PreferenceManager.getDefaultSharedPreferences(context).getInt(KEY_LAST_SELECTED_POSITION, 0)
    }

    private fun saveLastSelectedPosition(position: Int) {
        PreferenceManager.getDefaultSharedPreferences(context).edit().putInt(KEY_LAST_SELECTED_POSITION, position).apply()
    }
}


2022-09-22 14:49

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.