[英]How can I store multiple key pair values?
我目前正在關注有關以持久方式存儲數據的教程,以便將其保留在設備上,以便您可以在會話之間使用它。
本教程說明了用戶如何在主要活動中輸入姓名和電話號碼,然后將其傳遞到查看聯系人活動中。 這使用鍵值對。 這樣,即使重新啟動應用程序,用戶也可以查看其聯系人。
但是,每次用戶在主要活動中添加名稱和電話號碼時,以前的名稱和電話號碼都會被覆蓋。 當我要存儲所有輸入的聯系人時,僅允許我在列表中存儲一個聯系人。
有人可以幫忙嗎?
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
}
}
@ bamb094,希望該示例應用程序對您有幫助。
http://www.c-sharpcorner.com/article/serialization-and-deserialization-in-c-sharp/
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.