georges12
georges12

Reputation: 1

save and read tet from textview

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;

      
            
        
    }

Upvotes: 0

Views: 73

Answers (3)

georges12
georges12

Reputation: 1

I am use this code how is listening some values in the listview. My question , how i can edit the list items?

I try to make a calendar of 31 day where i can add expense for every day.

` public class MainActivity : ListActivity { protected override void OnCreate(Bundle bundle) { base.OnCreate(bundle);

        ListAdapter = new ArrayAdapter<string>(this, Resource.Layout.list_item, countries);

        ListView.TextFilterEnabled = true;

        ListView.ItemClick += delegate (object sender, AdapterView.ItemClickEventArgs args)
        {
            
            Toast.MakeText(Application, ((TextView)args.View).Text, ToastLength.Short).Show();
            //countries[1] = "ooin";
        };
    }


    public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
    {
        Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);

        base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
    }



    static readonly string[] countries = new String[] {
"Afghanistan","Albania","Algeria","American Samoa","Andorra",
"Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina",
"Armenia","Aruba","Australia","Austria","Azerbaijan",
"Bahrain","Bangladesh","Barbados","Belarus","Belgium",
"Belize","Benin","Bermuda","Bhutan","Bolivia",
"Bosnia and Herzegovina","Botswana","Bouvet Island","Brazil","British Indian Ocean Territory",
"British Virgin Islands","Brunei","Bulgaria","Burkina Faso","Burundi",    
  };



}

`

Upvotes: 0

georges12
georges12

Reputation: 1

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);          
    }

Upvotes: 0

Jessie Zhang -MSFT
Jessie Zhang -MSFT

Reputation: 13861

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;

    }
}

Upvotes: 0

Related Questions