Reputation: 377
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
Upvotes: 0
Views: 3062
Reputation: 1
DbHelper helper = new DbHelper(this);
btnInsert.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name, phone;
txtName = findViewById(R.id.txtName);
txtPhone = findViewById(R.id.txtPhone);
name = txtName.getText().toString();
phone = txtPhone.getText().toString();
if (!(name.equals("") && phone.equals(""))) {
Entity entity = new Entity();
entity.setName(name);
entity.setPhone(phone);
helper.insertData(entity);
} else {
Toast.makeText(MainActivity.this, "Fill the Empty Fields", Toast.LENGTH_LONG).show();
}
}
});
Upvotes: 0
Reputation: 1
public void insertData(Entity entity) {
SQLiteDatabase database = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, entity.getName());
values.put(KEY_PHONE, entity.getPhone());
database.insert(DATABASE_TABLE, null, values);
}
Upvotes: 0
Reputation: 1
public class Entity {
private int id;
private String name, phone;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
}
Upvotes: 0
Reputation: 235
Better to understand more about working with objects, and use of parcelables. Here is a very usefull tools that generate all code you need including Table, Model, classes for Insert and more:
http://www.codeproject.com/Tips/842150/Generator-for-SQLite-Use-in-Android
Create a table call Dic with Id, Name, Code In android for put EditText for name and code and onse save button, then onclick
dic.setName(edtName.getText().toString());
dic.setCode(edtCode.getText().toString());
database.Open()
database.insert(dic);
database.close();
If you want to show data from datebase to form:
Dic dic = new DIc();
database.open();
dic = database.findDic("id = 1");
database.Close;
lblName.setText(dic.getName().toString());
lblCode.setText(dic.getCode().toString());
Add one package for Models, and put each model in a seperate file here. Add one package for Db Add two file one as OpenHelper.Java, and one for Database.java for easy mangement of code
Upvotes: 2
Reputation: 1008
yes you can use Get Set method
public class GetSetClass {
// private variables
int _id;
String _data;
// Empty constructor
public GetSetClass() {
}
// constructor
public GetSetClass(int id, String data) {
this._id = id;
this._DataList_id = DataList_id;
}
// constructor
public GetSetClass(String data) {
this._data = data;
}
// getting ID
public int getID() {
return this._id;
}
// setting id
public void setID(int id) {
this._id = id;
}
public String getdata() {
return this._data;
}
public void setdata(String data) {
this._data = data;
}
}
see below code for set data using getset
public List<GetSetClass> getAllgetsets() {
List<GetSetClass> getsetList = new ArrayList<GetSetClass>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_CATEGORY;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
GetSetClass getset = new GetSetClass();
getset.setID(Integer.parseInt(cursor.getString(0)));
getset.setData(cursor.getString(1));
getsetList.add(getset);
} while (cursor.moveToNext());
}
// return getset list
return getsetList;
}
see below code for Get data using getset
public int updategetset(GetSetClass getset) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_data, getset.getData());
// updating row
return db.update(TABLE_CATEGORY, values, KEY_ID + " = ?",
new String[] { String.valueOf(getset.getID()) });
}
Upvotes: 4
Reputation: 7494
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.
Upvotes: 2