This question is abit similar to my shared preferences question only that it involves android database. i want to store a person salary in the database for the first time he start the app. the second time he launches it, the salary he stored will be viewed on the next activity as textview.
I roughly know how to skip the prompting of the salary but not sure about the database. my question is, how do i read it from the database that the salary is not null and display it on the next activity? i know that it somehow involves an if else statement like >> (if salary/database is not null, start intent to next activity as textview) but not sure how to code them.
(SavingsGuiderMenuActivity contains a link to SavingsGuiderAppActivity which ask for the salary(target) and then will display to the SavingsGuiderInserActivity activity. Means the user can only access the SavingsGuiderAppActivity once and the next time he launches it, from the SavingsGuiderMenuActivity will go directly to the SavingsGuiderInsertActivity)
Below are my codes SavingsGuiderMenuActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Bundle bundle = getIntent().getExtras();
String name= bundle.getString("name");
TextView resultView = (TextView)findViewById(R.id.view_Name);
resultView.setText("Welcome " + name);
ListView menuList = (ListView) findViewById(R.id.ListView_Menu);
String[] items = { getResources().getString(R.string.start),
getResources().getString(R.string.about),
getResources().getString(R.string.help) };
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.menu_item, items);
menuList.setAdapter(adapt);
menuList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View itemClicked, int position, long id) {
// Note: if the list was built "by hand" the id could be used.
// As-is, though, each item has the same id
TextView textView = (TextView) itemClicked;
String strText = textView.getText().toString();
if (strText.equalsIgnoreCase(getResources().getString(R.string.start))) {
if(!dataExists())
{
startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAppActivity.class));
}
else
{
Intent intent = new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderInsertActivity.class);
Bundle extras = new Bundle();
extras.putDouble("target",double_target);
extras.putInt("interval",int_interval);
intent.putExtras(extras);
startActivity(intent);
}
SavingsGuiderMenuActivity.this.finish();
}
else if (strText.equalsIgnoreCase(getResources().getString(R.string.help))) {
// Launch the Help Activity
startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderHelpActivity.class));
}
else if (strText.equalsIgnoreCase(getResources().getString(R.string.about))) {
// Launch the Settings Activity
startActivity(new Intent(SavingsGuiderMenuActivity.this, SavingsGuiderAboutActivity.class));
}
}//onitem click
});
}//bundle
public boolean dataExists()
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
Cursor cursor = null;
dbAdaptor.open();
cursor = dbAdaptor.getAllSavings();
if(cursor.moveToFirst())
{
double_target = cursor.getDouble(1);
int_interval = cursor.getInt(2);
return true;
}
else
{
return false;
}
}
Below are my codes SavingsGuiderAppActivity:
public class SavingsGuiderAppActivity extends SavingsActivity {
/** Called when the activity is first created. */
String tag = "Savings Guider";
EditText targetEdit, intervalEdit;
Button insertButton = null;
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.app);
targetEdit=(EditText)findViewById(R.id.edit_target);
intervalEdit=(EditText)findViewById(R.id.edit_interval);
insertButton = (Button) findViewById(R.id.btn_submit);
insertButton.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
DBAdapter dbAdaptor = new DBAdapter(getApplicationContext());
try{
dbAdaptor.open();
String target = targetEdit.getText().toString();
String interval = intervalEdit.getText().toString();
if(!target.equals("") && !interval.equals("")) {
try{
double target2 = Double.parseDouble(target);
int interval2 = Integer.parseInt(interval);
dbAdaptor.insertSavings(target2, interval2);
}
catch (NumberFormatException e) {
Context context = getApplicationContext();
CharSequence text = "Please enter valid value!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
else {
Context context = getApplicationContext();
CharSequence text = "Please do not leave any field blank!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
}
catch(Exception e){
Log.d("Savings Guider: ",e.getMessage());
}
finally{
if(dbAdaptor != null)
dbAdaptor.close();
}
}
});
}
}
Below are my codes for SavingsGuiderInsertActivity. i feel that theres something wrong with displaying the data i’ve inserted in the database from theSavingsGuiderAppActivity page. Like I shouldnt use Bundle Extras:
public class SavingsGuiderInsertActivity extends SavingsActivity {
/** Called when the activity is first created. */
String tag = "Savings Guider";
DecimalFormat df = new DecimalFormat("#.##");
protected void onCreate(Bundle savedInstance) {
super.onCreate(savedInstance);
setContentView(R.layout.insert);
Bundle bundle = getIntent().getExtras();
double target= bundle.getDouble("target");
int interval= bundle.getInt("interval");
TextView targetView = (TextView)findViewById(R.id.view_target);
TextView intervalView = (TextView)findViewById(R.id.view_interval);
targetView.setText("$" + target);
intervalView.setText("" + interval);
double average = target/interval;
TextView averageView = (TextView)findViewById(R.id.viewAverage);
averageView.setText("You must save $" + df.format(average) + " everytime!");
}
You could do it this way.
Create a database table during the first launch, but the table will be empty.
Check if the table is empty, and do nothing. Else grab the data from the database and display them.
You can manage Android database using SQLiteOpenHelper.
In order to check if the table is empty, you could do something like this