2

first I have to mention that I'm new to Android app development. I'm using Xamarin (C#) for developing apps. Here's the problem. I want to write simple ToDoList application to learn how to store/share data between activities.

Button "Add" is placed in Main layout. When user clicks it, another screen shows up where user can name(EditText) task and add its description(EditText). When user clicks button "Complete" in that screen, app redirects user to Main screen and add user's task to a listview.

I did some research and found out that I could do that with SharedPreferences, saving data to file on device, or sql.

I decided to go with SharedPreferences. Problem is, everything I found about SharedPrefences (and I searched a lot, believe me) is written in Java which wouldn't be such a big problem, but nobody ever mentioned sharing data between different activites using shared preferences. My code is below, I'm really struggling with this over a day, so please help me.

My MainActivity (where button "Add" is):

    public class MainActivity : Activity
{
    Button add;
    ListView list;

    protected override void OnCreate(Bundle bundle)
    {


        base.OnCreate(bundle); 

        ///set layout
        SetContentView(Resource.Layout.Main);

        ///define variables, buttons, lists
        add = FindViewById<Button>(Resource.Id.add);
        list = FindViewById<ListView>(Resource.Id.list);

        ///get string from secondActivity
        var prefs = Application.Context.GetSharedPreferences("todolist", FileCreationMode.Private);
        var preference = prefs.GetString("task", null);

        ///add string (preference) to list
        List<string> lista = new List<string>();
        lista.Add(preference);

        ///"convert" list to listview
        ArrayAdapter<string> adapter = new ArrayAdapter<string>(this, Resource.Layout.Main, lista);
        list.Adapter = adapter;

        add.Click += delegate
        {
            StartActivity(new Intent(this, typeof(addActivity)));
        };     



    }       

}

My second screen (second activity):

    public class addActivity : Activity
{

    /// define variables and create list
    Button complete;
    EditText name, description;

    protected override void OnCreate(Bundle bundle)
    {
        base.OnCreate(bundle);
        SetContentView(Resource.Layout.add);

        ///variables
        complete = FindViewById<Button>(Resource.Id.complete);
        name = FindViewById<EditText>(Resource.Id.name);
        description = FindViewById<EditText>(Resource.Id.description);

        ///converting to string
        string title = name.Text.ToString();
        string content = description.Text.ToString();

        ///pass string to MainActivity
        complete.Click += delegate
        {
            if (String.IsNullOrEmpty(title))
            {
                Toast.MakeText(this, "Please enter task name", ToastLength.Short).Show();
            }
            else
            {
                var prefs = Application.Context.GetSharedPreferences("todolist", FileCreationMode.Private);
                var prefEditor = prefs.Edit();
                prefEditor.PutString("task", title);
                prefEditor.Commit();
                Toast.MakeText(this, "Task added", ToastLength.Short).Show();
                StartActivity(new Intent(this, typeof(MainActivity)));

            }
        };


    }

}
matejcro
  • 79
  • 1
  • 7

2 Answers2

2

You could use an SQLite database here. I would recommend doing so if you want to build a ToDo list. It will make it easier for you as well.

Xamarin has plenty of documentation on how to set this up here:

https://developer.xamarin.com/guides/cross-platform/application_fundamentals/data/

They also have an example app that does pretty much what you want to do here:

https://github.com/xamarin/mobile-samples/tree/master/Tasky

Daniel Hakimi
  • 1,153
  • 9
  • 16
0

You can pass the string to the next activity through an Intent

Instead of defining that intent in the arg of your Start activivty you should define it before that and then add the string using the putExtra method

examples: How to use putExtra() and getExtra() for string data

Community
  • 1
  • 1
Will Evers
  • 934
  • 9
  • 17
  • the thing is I want to save data (tasks/strings). I read somewhere that Intent just send strings, it doesn't store it for the next time user open application. Correct me if I'm wrong. – matejcro Dec 07 '15 at 19:50
  • There are many ways to save data like this. The android website outlines a few here: http://developer.android.com/guide/topics/data/data-storage.html Please mark my answer as correct if the information provided answers your questions – Will Evers Dec 07 '15 at 19:52
  • I've read it over and over couple of times, but I can't find examples of implementation of shared preferences/internal storage/external storage in C#. So please, if you know how to do what I'm asking using shared preferences in C#, I'll mark your answer as correct. I know what you're trying to say, but I don't know how to use it in C#. Thanks in advance. – matejcro Dec 07 '15 at 20:01