Using Buttons

So far I been able to get buttons to load views and that's it so far. Well I tried to setup a button that displays a messages. and when I click the button, Nothing happens. I have other options that it should run. Until I get this button fixed. ?

My code is below.

`

Button btnSaveContact = FindViewById (Resource.Id.btnSaveContact);

btnSaveContact.Click += delegate {

            String toastMessage1 = "Saving, Please wait"; // This should show and doesn't
            Toast.MakeText(this, toastMessage1, ToastLength.Short);

            //try
            //{
                //String toastMessage2 = "Contact Saved";
                //Toast.MakeText(this, toastMessage2, ToastLength.Short);

                //Contacts contact = new Contacts(-1, fullName, email, phone);
                //DBContact.SaveContact(contact); // correct way

            //}
            //catch(Exception ee)
            //{
                //String toastMessage3 = "Error Saving Contact : " + ee.ToString();
                //Toast.MakeText(this, toastMessage3, ToastLength.Long);

            //}
        };

`

Does any one have any ideas.

Posts

  • rmaciasrmacias USBeta, University ✭✭✭✭✭
    edited September 2013

    Yes,

    Toast.MakeText(this, toastMessage1, ToastLength.Short);

    Should be:

    Toast.MakeText(this, toastMessage1, ToastLength.Short).Show();

    And even then, you may have to run it on the UI thread.

    RunOnUiThread(() => Toast.MakeText(this, toastMessage1, ToastLength.Short).Show());

  • bigjoe11abigjoe11a USMember

    oops, I didn'tr see the show() was missing, Sorry about that. Now that I now the button is working. Any one have any idea why nothing works. lol...

    When I click this button. It should insert a new record into the database and well as far as I can see it doesn't even make the SQLite database. So now I'm so lost on this.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    Yes, you should uncomment the code that makes your database calls.

  • bigjoe11abigjoe11a USMember

    Sorry, You don't under stand. I mean even when I did have it uncommented the code didn't work. I checked the code and as far as I know I didn't have a Personal folder on my PC that I know of.

    So I had to use #if (DEBUG) to tell if I was in debug mode or just running the app. I all so had to make a member to my DBContact Class. To run the GetConnected() and Createdatabase() methods.

    before I tried to save a new record to see if that will work.

  • rmaciasrmacias USBeta, University ✭✭✭✭✭

    I know, I was just messing with you. The Personal Folder is not on your PC, it is on your device/emulator. The Personal folder also is the application's private folder. For security reasons, you won't be able to navigate to that folder using a file manager on the Android OS, but it is there.

    Also, make sure you have "Preserve data/cache between deployments" checked. If not, everytime you deploy/debug, your DB will get wiped out.

    Do you have code that saves to your DB, and then reads from it to verify that the data has been written?

  • bigjoe11abigjoe11a USMember

    How do I do this

    Also, make sure you have "Preserve data/cache between deployments" checked. If not, everytime you deploy/debug, your DB will get wiped out.

    1) Do you have code that saves to your DB,
    Yes. Sense I have no idea is the DB has been created. I don't know
    2) and then reads from it to verify that the data has been written?
    No. Let me show you some code.

    This is what I use to create the database and tables. I have a class called DBContact.
    This checks the database
    `
    rivate static SqliteConnection GetConnection ()
    {
    var dbPath = Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), db_file);
    bool exists = File.Exists (dbPath);

            if (!exists) 
                SqliteConnection.CreateFile (dbPath);               
    
            var conn = new SqliteConnection ("Data Source=" + dbPath);
    
            if (!exists)
                CreateDatabase (conn);
    
            return conn;
        }
    

    `

    The next code adds tables and adds a new default record

    `

    private static void CreateDatabase (SqliteConnection connection)
    {
    var sql = "CREATE TABLE CONTACTS (Id INTEGER PRIMARY KEY AUTOINCREMENT,Id int, fullname string, email string, phone string);";

            connection.Open ();
    
            using (var cmd = connection.CreateCommand ()) {
                cmd.CommandText = sql;
                cmd.ExecuteNonQuery ();
            }
    
            // Create a sample Contact to get the user started
            sql = "INSERT INTO CONTACTS (Id, fullname, email, phone) VALUES (@Id, @fullname, @email, @phone);";
    
            using (var cmd = connection.CreateCommand ()) {
                cmd.CommandText = sql;
                cmd.Parameters.AddWithValue ("@fullname", "Joe Moody");
                cmd.Parameters.AddWithValue ("@email", "[email protected]");
                cmd.Parameters.AddWithValue ("@phone", "000-000-0000");
    
                cmd.ExecuteNonQuery ();
            }
    
            connection.Close ();
        }
    

    `

    and last here's the code to save my record.

    `
    public static void SaveContact (Contacts contact)
    {
    using (var conn = GetConnection ()) {
    conn.Open ();

                using (var cmd = conn.CreateCommand ()) {
    
                    if (contact.Id < 0) {
                        // Do an insert
                        cmd.CommandText = "INSERT INTO CONTACTS (fullname, email, phone) VALUES (@fullname, @email, @phone); SELECT last_insert_rowid();";
                        cmd.Parameters.AddWithValue ("@fullname", contact.Fullname);
                        cmd.Parameters.AddWithValue ("@email", contact.Email);
                        cmd.Parameters.AddWithValue ("@phone", contact.Phone);
    
                        contact.Id = (long)cmd.ExecuteScalar ();
                    } else {
                        // Do an update
                        cmd.CommandText = "UPDATE CONTACTS SET fullname = @fullname, email = @email, phone = @phone WHERE Id = @Id";
                        cmd.Parameters.AddWithValue ("@fullname", contact.Fullname);
                        cmd.Parameters.AddWithValue ("@email", contact.Email);
                        cmd.Parameters.AddWithValue ("@phone", contact.Phone);
    
                        cmd.ExecuteNonQuery ();
                    }
                }
            }
        }
    

    `

    I hope this helps you so you can help me. Right now I'm not sure if I will continue to make apps.

  • bigjoe11abigjoe11a USMember

    You forgot to tell me how to set the files when deployed. and well I fount it. Just took me a while so I wanted to tell every one else

    In Xamarin Studio go to Tools->Options. ... Projects->Android, make sure the "Preserve data/cache between application deploys" is checked.

    This way your files shouldn't get deleted.

Sign In or Register to comment.