2017-12-06 12 views
0

私は現在、データを永続的な方法で保存するチュートリアルに従っています。そのため、セッション間で使用できるように、デバイスに保存されます。複数のキーペア値を保存するにはどうすればよいですか?

このチュートリアルでは、メインアクティビティ内で名前と電話番号を入力する方法を示しています。その後、ビューの連絡先アクティビティに渡されます。これは、キーと値のペアを使用します。これにより、ユーザーは、アプリを再起動しても連絡先を表示できます。

ただし、ユーザーがメインアクティビティ内で名前と電話番号を追加するたびに、以前の名前と電話番号が上書きされます。入力されたすべての連絡先を保存するときに、私のリスト内に1つの連絡先のみを保存することができます。

誰でも手助けできますか?

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 

     } 
    } 
} 

。 bamb094 @

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 

    } 
} 
+0

こんにちは、この目的のためにSQLiteを使用してください。データをテーブルに格納し、クエリを使用して同じデータセットを取得します。 [リンク](https://developer.xamarin.com/recipes/android/data/databases/sqlite/)SQLiteデータベースの実装。 –

+0

JSON文字列またはその他のデータ形式としてシリアル化してから、それをシリアル番号に戻して – Baksteen

+0

@Baksteenの方法に戻ってみてください。私は、これまでに行ってきた最初のチュートリアルであるため、永続的な方法でデータを保存するのは非常に新しいです。 – bamb094

答えて

0

、このサンプルアプリケーションは、あなたの場合に役立つことを願っています。

関連する問題