Issue
I have a table on a database with two columns, and then i am trying to use webservice,JSON parse some data from some URL to the database and save it. I try to do that in this method
public void saveDataRecord(String id, String name) {
ContentValues contentValues = new ContentValues();
contentValues.put(CATEGORY_COLUMN_ID, id);
contentValues.put(CATEGORY_COLUMN_NAME, name);
database.insert(TABLE_NAME, null, contentValues);
}
However i am also having model class(which contain setter and getter), that can be use when i try to read data from database using list(it works on my previous object that have database inside and the values already inserted). Now i want to use that because it has the parameters and can be useful to read list(take a look at the bottom code). I am a bit confuse, what to use getId,getName or setId,setName?
this?
public void saveDataRecord(TheModelClass blablabla) {
ContentValues contentValues = new ContentValues();
contentValues.put(CATEGORY_COLUMN_ID, blablabla.getId());
contentValues.put(CATEGORY_COLUMN_NAME, blablalbla.getName());
database.insert(TABLE_NAME, null, contentValues);
}
or this?
public void saveDataRecord(TheModelClass blablabla) {
ContentValues contentValues = new ContentValues();
contentValues.put(CATEGORY_COLUMN_ID, blablabla.setId());
contentValues.put(CATEGORY_COLUMN_NAME, blablalbla.setName());
database.insert(TABLE_NAME, null, contentValues);
}
even tho i did this on my other project
public List<TheModelClass> getAllEffectiveRates() {
List<TheModelClass> EffectiveRates = new ArrayList<TheModelClass>();
String selectQuery = "SELECT * FROM " + TABLE_EFFECTIVE_RATE;
SQLiteDatabase db = this.getReadableDatabase();
Cursor c = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (c.moveToFirst()) {
do {
TheModelClass example = new TheModelClass();
example.setERId(c.getInt(c.getColumnIndex(KEY_ER_ID)));
example.setERTenor(c.getInt(c.getColumnIndex(KEY_ER_TENOR)));
example.setERRate(c.getDouble(c.getColumnIndex(KEY_ER_RATE)));
// add
EffectiveRates.add(example);
} while (c.moveToNext());
}
// db.close();
c.close();
return EffectiveRates;
}
I am still confuse with what getter and setter do exactly
Solution
Getter basically returns the data held by that instance. Setter sets the data to be held by the instance.
In your case, while inserting data, you should use the getters as the values in the ContentValues instance. So it should be:
public void saveDataRecord(TheModelClass blablabla) {
ContentValues contentValues = new ContentValues();
contentValues.put(CATEGORY_COLUMN_ID, blablabla.getId());
contentValues.put(CATEGORY_COLUMN_NAME, blablalbla.getName());
database.insert(TABLE_NAME, null, contentValues);
}
Getters will return a value. Setters (in general) will not return a value. So if you use the setter, you will only be setting the value of the instance in ContentValues but you will not actually be using it as the call will not return anything.
Answered By - ucsunil
Answer Checked By - Katrina (JavaFixing Volunteer)