Home > Software engineering >  save and read tet from textview
save and read tet from textview

Time:11-06

Can I save values ​​in textview that appear again after restarting the application?

is there a possibility?

namespace App11 { [Activity(Label = "@string/app_name", Theme = "@style/AppTheme.NoActionBar", MainLauncher = true)] public class MainActivity : AppCompatActivity {

    EditText edt;
    TextView res;
 
    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        Xamarin.Essentials.Platform.Init(this, savedInstanceState);
        SetContentView(Resource.Layout.activity_main);

        edt = FindViewById<EditText>(Resource.Id.appCompatEditText1);
        Button btn = FindViewById<Button>(Resource.Id.button1);
        res = FindViewById<TextView>(Resource.Id.textView10);

        btn.Click  = Btn_Click;

           
    }

    private void Btn_Click(object sender, System.EventArgs e)
    {
        string s = edt.Text.ToString();
        res.Text = s;

      
            
        
    }

CodePudding user response:

A simple way is to use ISharedPreferences .

You can save the value while clicking the button in your page and retrieve this value once resume your page.

You can refer to the following code:

public class MainActivity : Activity 
{

    EditText edt;
    TextView res;
    protected override void OnCreate(Bundle? savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        // Set our view from the "main" layout resource
        SetContentView(Resource.Layout.activity_main);

        edt = FindViewById<EditText>(Resource.Id.editText1);
        Button btn = FindViewById<Button>(Resource.Id.button1);
        res = FindViewById<TextView>(Resource.Id.textView1);

        btn.Click  = Btn_Click;
    }

    private void Btn_Click(object sender, System.EventArgs e)
    {
        string s = edt.Text.ToString();
        res.Text = s;


        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        ISharedPreferencesEditor editor = prefs.Edit();
        editor.PutString("key_for_my_str_value", s);
        // editor.Commit();    // applies changes synchronously on older APIs
        editor.Apply();        // applies changes asynchronously on newer APIs


    }

    protected override void OnResume()
    {
        base.OnResume();

        ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
        string mString = prefs.GetString("key_for_my_str_value", "");
        res.Text =mString;

    }
}

CodePudding user response:

i tried the code below, and always get this error:

"System.IO.DirectoryNotFoundException: 'Could not find a part of the path "/c:/text.txt".'"

    protected override void OnCreate(Bundle bundle)
    { 
       EditText edt;

        edt = FindViewById<EditText>(Resource.Id.listView1);

        using (var sr = new StreamReader(Path.Combine(Environment.CurrentDirectory, "c:/text.txt")))
        {
            while (!sr.EndOfStream)
            {
                string fileLine = sr.ReadLine();
                foreach (string piece in fileLine.Split(','))
                {
                    edt = EditText(piece);

                    sr.Close();
                }
            }
        }

        base.OnCreate(bundle);          
    }
  • Related