0

I'm creating my first data enabled app but am struggling with the last part - actually adding and retrieving the data. For now I am only concerned with adding data to the table. Following this tutorial I have created model classes for my tables and a DBHelper class with all my CRUD methods (I can post all these if required but not sure they are necessary to answer this question. Please correct me if I am wrong!). Unfortunately the tutorial ends here and doesn't go into detail on how to pass the data from the UI of the app into the DB.

After some Google searches I have found an example of how to pass some data to these methods but only how to pass one piece of data at a time, so only really useful if my table has just one field - mine has more than one.

For example, if I have a a table for "Todo" tasks, in my dbhelper my create method may be;

public void createTodo(String todoText) {
    ContentValues contentValues = new ContentValues();
    contentValues.put("todo", todoText);
    // Insert into DB
    db.insert("todos", null, contentValues);
}

so from my activity I just need

dao.createTodo(todoTextValue);

For my app I will be adding more than one field at a time, so my create method looks like this;

public long createSite(Site site){
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_SITE_NAME, site.getSiteName());
    values.put(KEY_SITE_LAT, site.getSiteLat());
    values.put(KEY_SITE_LON, site.getSiteLon());
    values.put(KEY_CREATED_AT, site.getSiteCreatedDateTime());

    // Insert Row
    long siteid = database.insert(TABLE_SITES, null, values);

So my question really is how I can pass all the different bits of data to the createSite method.

8
  • What do you mean by the "create" method? The "createSite" method? Commented Jan 23, 2016 at 12:18
  • I'm unclear as to what you're asking, exactly. It looks like you've got it figured out. Commented Jan 23, 2016 at 12:18
  • Apologies, yes exactly Commented Jan 23, 2016 at 12:18
  • Say I have 4 x EditText in MainActivity, how to I pass this data to the DB? Commented Jan 23, 2016 at 12:19
  • 1
    create a new site object using the new Site() call. if you can pass the variables into the constructor thats good. Otherwise, call the setters defined in the class. If there aren't any defined already, then you will have to define them Commented Jan 23, 2016 at 12:44

3 Answers 3

1

I don't know if this really needs an answer, but well here's a code...

Assuming your Site class is like this.

    public class Site {
        private String siteName;
        private double siteLat;
        private double siteLon;
        private Date siteCreatedDateTime;

        // getters and setters here
    }

You then pass the data from your EditText value to your new Site object. It will look like this in your activity.

EditText siteNameInput = (EditText) findViewById(R.id.siteNameInput);
EditText siteLatInput = (EditText) findViewById(R.id.siteLatInput);
EditText siteLonInput = (EditText) findViewById(R.id.siteLonInput);
EditText siteCreatedDateTimeInput = (EditText) findViewById(R.id.siteCreatedDateTimeInput);

String siteName = siteNameInput.getText().toString();
String siteLat = siteLatInput.getText().toString();
String siteLon = siteLonInput.getText().toString();
String siteCreatedDateTime= siteCreatedDateTimeInput.getText().toString();

Site site = new Site();
site.setSiteName(siteName);
site.setSiteLat(siteLat);
site.setSiteLon(siteLon);
site.setSiteCreatedDateTime(siteCreatedDateTime);

dao.createSite(site);

Hope this helps you... You can learn more on Object-Oriented programming in Java here

Sign up to request clarification or add additional context in comments.

Comments

1
public long createSite(Model site,String name, String address){
    SQLiteDatabase database = this.getWritableDatabase();

    ContentValues values = new ContentValues();

    values.put(KEY_NAME, site.name);
    values.put(KEY_ADDRESS, site.address);

    // Insert Row
    long siteid = database.insert(TABLE_SITES, null, values);

to add elements to the class you just add

public class Model {
        String name;
        String address;
        //add year as many as you need
        Model(String name, String address){
             this.name=name;
             this.address=address;
        }


    }

And in you activity you call this In java to add a new object in this case Model

Model x = new Model("josh","Ireland");

and you just pass to

dao.createTodo(x);

5 Comments

what's the point of having the model if you're not going to use it and just pass the String values separately...?
yes he can create a model but if he is asking how to pass elements, is because I don't want to create one object.
"Site" IS the model. He just needs to add variables to it.
As mentioned above in another comment I'm just a hobbyist so I think my lack of using proper terminology doesn't help. I need to know how to add variable to the model.
@JonnyWright see my answer now, I edit all. for all that you need
0

Todo and Site are models. Each variable represents a column of that table. You need to create a custom model for each of your tables. The createSite method takes an object of type Site and adds it as a row in the TABLE_SITES in the DB. values.put(...)takes columnName, value. So here you give your own column names and values.

Instead of getting into all this I suggest you use an orm like active android: http://www.activeandroid.com/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.