如何存储多个密钥对值?

时间:2017-12-06 13:21:19

标签: c# android xamarin shared preference

我目前正在关注以持久方式存储数据的教程,以便它将保留在设备上,以便您可以在两次会话之间使用它。

教程显示用户如何在主活动中输入姓名和电话号码,然后将其传递到视图联系人活动。这使用键值对。这样,即使应用程序重新启动,用户也可以查看其联系人。

但是,每次用户在主活动中添加姓名和电话号码时,都会覆盖以前的姓名和电话号码。当我想存储所有已输入的联系人时,允许我只在我的列表中存储一个联系人。

有人可以帮忙吗?

MainActivity.cs

using Android.App;
using Android.Widget;
using Android.OS;
using Android.Content;

namespace Contacts
{
    [Activity(Label = "Contacts", MainLauncher = true, Icon = "@mipmap/icon")]
    public class MainActivity : Activity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

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




            // Get our button from the layout resource,
            // and attach an event to it



            Button button = FindViewById<Button>(Resource.Id.submitButton);

            button.Click += delegate
            {

                EditText nameBox = FindViewById<EditText>(Resource.Id.nameBox);
                string name = nameBox.Text;
                EditText phoneBox = FindViewById<EditText>(Resource.Id.phoneBox);
                string phone = phoneBox.Text;


                //add the new contact to the share preferrences

                var localContacts = Application.Context.GetSharedPreferences("MyContacts", FileCreationMode.Private);
                //Private file creation mode, which means that the data can't be accessed by any other app on the phone  

                var contactEdit = localContacts.Edit(); // takes the shared preferences and tells it that we want to edit


                //this uses KeyValue pairs
                contactEdit.PutString("Name", name);
                contactEdit.PutString("Phone", phone);
                contactEdit.Commit(); //writes the shared preferences to the device

                //contactEdit.PutStringSet()


                // create a toast notification to confirm the submission

                Android.Widget.Toast.MakeText(this, "Item Added", ToastLength.Short).Show();

                //clear the boxes of the text

                nameBox.Text = "";
                phoneBox.Text = "";




            };


            //Button viewContactButton = FindViewById<Button>(Resource.Id.viewContactButton);

            //viewContactButton.Click += (sender, e) =>
            //{
            //   var intent = new Intent(this,)
            //    StartActivity(Intent);





            //};


            Button btnContactButton = FindViewById<Button>(Resource.Id.viewContactButton);

            btnContactButton.Click += delegate {


                StartActivity(typeof(ViewContactsActivity));
            };






        }
    }
}

ViewContactsActivity.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;

namespace Contacts
{
    [Activity(Label = "ViewContactsActivity")]

    public class ViewContactsActivity : ListActivity
    {
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            SetContentView(Resource.Layout.ViewContacts);

            // retrieve the information from shared preferences
            //referencing the same file
            var localContacts = Application.Context.GetSharedPreferences("MyContacts", FileCreationMode.Private);


            string name = localContacts.GetString("Name", null);
            string phone = localContacts.GetString("Phone", null);


            //call the constructor, to overrride the string to control how it looks when you display it

            Contact myContact = new Contact(name, phone);

            //create an array of items that will go in the list

            Contact[] contactList = { myContact };

            //add the list to the list adapter


            ListAdapter = new ArrayAdapter<Contact>(this, Android.Resource.Layout.SimpleListItem1, contactList);

            //contact list is the item we want to display

        }
    }
}

使用System;

Contact.cs

namespace Contacts
{
    class Contact
    {

        public string Name { get; set; }
        public string PhoneNumber { get; set; }

        public Contact(string name, string phone)
        {

            Name = name;
            PhoneNumber = phone;
        }


        public override string ToString()
        {
            return Name + "" + PhoneNumber;
        }

        // this chooses how the information will be displayed

    }
}

1 个答案:

答案 0 :(得分:0)

@ bamb094,希望这个示例应用程序对您的案例有所帮助。

http://www.c-sharpcorner.com/article/serialization-and-deserialization-in-c-sharp/