I have an application that takes a lot of user input across various fragments and activities. Right now I have all the variables in one class lets call it "Report" and I pass around the primary key to know exactly what "Report" I am in. (there is a ListView at the start where the user selects what Report they want to make changes to) Since all the user input eventually has to be written to the database I had an idea - why not remove all the variables from the Report and replace them with static final column names, leaving only the primary key variable, then use the getters and setters to read from and write directly to the database? Would this be a good design to use or are there any drawbacks I am missing? As a relatively inexperienced android developer I am asking for any advice I can get.
Example (sorry if there are any mistakes I am writing this from a phone):
public class Report {
private int primaryKey;
private SQLiteDBHelper db;
private static final String ColumnA = "columnA";
private static final String ColumnB = "columnB";
private static final String ColumnC = "columnC";
// lets say columns A and B are Strings and C is an int
public Report(int primaryKey, Context ctx) {
this.primaryKey = primaryKey;
this.db = new SQLiteDBHelper(ctx);
}
public String getColumnA() {
return db.getString(primaryKey, ColumnA)
}
public boolean setColumnA(String value) {
return db.setString(primaryKey, ColumnA, value);
}
public String getColumnB() {
return db.getString(primaryKey, ColumnB)
}
public boolean setColumnB(String value) {
return db.setString(primaryKey, ColumnB, value);
}
public String getColumnC() {
return String.valueOf(db.getInt(primaryKey, ColumnC));
}
public boolean setColumnC(String value) {
return db.setInt(primaryKey, ColumnC, Helper.tryParseInt(value));
}
}
I could then extend TextWatcher like this:
public MyTextWatcher extends TextWatcher {
private View view;
private Report report;
public MyTextWatcher(View view, Report report) {
this.view = view;
this.report = report;
}
public void afterTextChanged(Editable editable) {
String input = editable.toString();
switch(view.getId()) {
case R.id.columnA:
if (!report.setColumnA(input)) {
// something went wrong
}
... etc.
}
}
And in the activity/fragment I would do this
int primaryKey = get from shared pref;
Report report = new Report(primaryKey, getApplication());
EditText columnA = (EditText) v.findViewById(R.id.columnA);
EditText columnB = (EditText) v.findViewById(R.id.columnB);
columnA.setText(report.getColumnA());
columnB.setText(report.getColumnB());
columnA.addTextChangedListener(new MyTextWatcher(columnA, report));
columnB.addTextChangedListener(new MyTextWatcher(columnB, report));
Why I think this is a good idea:
I can save the current primaryKey (the one user selected from ListView) in the SharedPreferences and then make a new Report from it in every activity/fragment I need to use it, without using up much memory
I can make sure there is never any discrepency between what the user entered and what is in the DB - for example on EditTexts i could call the corresponding setter for every column in the OnTextChanged listener
for every activity/fragment only the fields that are actually displayed on the screen would be retrieved from the database in onCreate
less code? ( for eg. I only need get/setInt and get/setString methods in my DB helper, I dont need to pass anything around between activities/fragments, only need one TextWatcher class)
Why I think this approach might be bad:
constant reading and writing to db - battery usage and speed might be problems?
???