【问题标题】:How can I store multiple key pair values?如何存储多个密钥对值?
【发布时间】:2017-12-06 13:21:18
【问题描述】:

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

本教程展示了用户如何在主活动中输入姓名和电话号码,然后将其传递到查看联系人活动中。这使用键值对。这让用户可以查看他们的联系人,即使在应用重新启动时也是如此。

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

谁能帮忙?

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

        }
    }
}

使用系统;

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

    }
}

【问题讨论】:

  • 您好,请使用 SQLite。将数据存储到表中并使用查询检索相同的数据集。 Link 用于 SQLite 数据库实现。
  • 您可以尝试将其序列化为 JSON 字符串或其他数据格式,然后将其序列化回首选项
  • @Baksteen 怎么样,我会去吗?我对以持久方式存储数据非常陌生,因为这是我遵循的第一个教程
  • 我真的不知道,我没有使用 Xamarin 的经验。难道您不只是将值存储为电话号码数组吗?
  • 当你存储值时,你应该使用不同的key来存储不同的值。

标签: c# android xamarin shared preference


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2023-03-08
    • 2020-08-14
    • 2013-02-07
    • 2021-09-07
    • 2022-10-23
    • 1970-01-01
    • 2017-09-14
    • 1970-01-01
    • 2020-09-10
    相关资源
    最近更新 更多