- Forum posts: 1
Apr 29, 2014, 8:36:27 PM via Website
Apr 29, 2014 8:36:27 PM via Website
I am new to programming an am a novice. I am trying to write a program that takes some variables and updates entries in my database using an asynctask. As i've mentioned I am a novice and have no idea what to do.
Background: This code shows that I an receiving "variables/extras" from a previous intent. I am using them and storing them into TextView. Now I want to update the Miles/Hours/Cost columns of my database. Can anyone help me? Please? When I run the code it's as though the asynctask isn't there at all.
Breakpoints set at:
new SummaryAsyncTask().execute((Void) null);
class SummaryAsyncTask extends AsyncTask {
protected Boolean doInBackground(Void... params) {
LogCat output:
04-29 13:23:12.912: I/ActivityManager(380): START u0 {cmp=com.example.dissertation/.FinalQuotePage (has extras)} from pid 5548 04-29 13:23:13.232: D/dalvikvm(5548): GC_FOR_ALLOC freed 1356K, 25% free 6119K/8136K, paused 43ms, total 49ms 04-29 13:23:14.062: D/dalvikvm(5548): GC_FOR_ALLOC freed 1082K, 25% free 6154K/8136K, paused 53ms, total 74ms 04-29 13:23:14.882: I/======= Hours(5548): :: 13 04-29 13:23:15.562: W/EGL_emulation(5548): eglSurfaceAttrib not implemented 04-29 13:23:17.102: I/ActivityManager(380): Displayed com.example.dissertation/.FinalQuotePage: +3s714ms
mysql_query ("UPDATE Newtest22 SET Miles='$miles', Hours='$hours', Cost='$cost' WHERE FirstName='$firstname' and LastName='$lastname' and Email='$email'"
@SuppressLint("SimpleDateFormat" public class FinalQuotePage extends Activity {
TextView tvFirstName, tvLastName, tvEmail, tvMiles, tvDepartTime, tvReturnTime, tvCoachesRequired, tvHours, tvCost;
TextView tvFirstNameHidden, tvLastNameHidden;
int milesa, coaches, cost, hoursb;
String dbmilesValue;
String dbhoursValue;
String dbcostValue;
String dbfirstnameValue;
String dblastnameValue;
String dbemailValue;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final_quote_page);
Bundle bundle=getIntent().getExtras(); //create a bundle object to store
String firstname=bundle.getString("firstname"
tvFirstName=(TextView)findViewById(R.id.tvFirstName);
tvFirstNameHidden=(TextView)findViewById(R.id.tvFirstNameHidden);
tvFirstNameHidden.setText(""+firstname);
String lastname=bundle.getString("lastname"
tvLastName=(TextView)findViewById(R.id.tvLastName);
tvFirstName.setText(""+firstname +lastname);
tvLastNameHidden=(TextView)findViewById(R.id.tvLastNameHidden);
tvLastNameHidden.setText(""+lastname);
String email=bundle.getString("email"
tvEmail=(TextView)findViewById(R.id.tvEmail);
tvEmail.setText(""+email);
String miles=bundle.getString("miles"
tvMiles=(TextView)findViewById(R.id.tvMiles);
tvMiles.setText(""+miles);
String departtime=bundle.getString("departtime"
tvDepartTime=(TextView)findViewById(R.id.tvDepartTime);
tvDepartTime.setText(""+departtime);
String returntime=bundle.getString("returntime"
tvReturnTime=(TextView)findViewById(R.id.tvReturnTime);
tvReturnTime.setText(""+returntime);
String coachesrequired=bundle.getString("coachesrequired"
tvCoachesRequired=(TextView)findViewById(R.id.tvCoachesRequired);
tvCoachesRequired.setText(""+coachesrequired);
tvHours=(TextView)findViewById(R.id.tvHours);
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("HH:mm"
Date date1 = null;
try {
date1 = simpleDateFormat.parse(""+ departtime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Date date2 = null;
try {
date2 = simpleDateFormat.parse(""+ returntime);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
long difference = date2.getTime() - date1.getTime();
int days = (int) (difference / (1000*60*60*24));
int hours = (int) ((difference - (1000*60*60*24*days)) / (1000*60*60));
int min = (int) (difference - (1000*60*60*24*days) - (1000*60*60*hours)) / (1000*60);
hours = (hours < 0 ? -hours : hours);
Log.i("======= Hours"," :: "+hours);
tvHours.setText("" + hours);
tvCost=(TextView)findViewById(R.id.tvCost);
String hoursa = tvHours.getText().toString();
try {
milesa = Integer.parseInt(miles);
coaches = Integer.parseInt(coachesrequired);
hoursb = Integer.parseInt(hoursa);
}
catch(NumberFormatException nfe)
{
}
int milescalc = (int) (milesa * 1.70);
int hourscalc = (int) (hoursb * 18.00);
int sum = (int) (milescalc + hourscalc);
int cost = (int) sum * coaches;
tvCost.setText("£" + cost);
dbfirstnameValue = tvFirstNameHidden.getText().toString();
dblastnameValue = tvLastNameHidden.getText().toString();
dbemailValue = tvEmail.getText().toString();
dbmilesValue = tvMiles.getText().toString();
dbcostValue = tvCost.getText().toString();
dbhoursValue = tvHours.getText().toString();
if(tvFirstName != null || !tvFirstName.getText().equals("")
{
new SummaryAsyncTask().execute((Void) null);
}else {
//null or empty
}
}
class SummaryAsyncTask extends AsyncTask<Void, Void, Boolean> {
private void postData(String dbmiles, String dbhours, String dbcost, String dbfirstname, String dblastname, String dbemail)
{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("*php address here*"
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(6);
nameValuePairs.add(new BasicNameValuePair("FirstName", dbfirstname));
nameValuePairs.add(new BasicNameValuePair("LastName", dblastname));
nameValuePairs.add(new BasicNameValuePair("Email", dbemail));
nameValuePairs.add(new BasicNameValuePair("Miles", dbmiles));
nameValuePairs.add(new BasicNameValuePair("Hours", dbhours));
nameValuePairs.add(new BasicNameValuePair("Cost", dbcost));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
}
catch(Exception e)
{
Log.e("log_tag", "Error: "+e.toString());
}
}
@Override
protected Boolean doInBackground(Void... params) {
postData(dbfirstnameValue, dblastnameValue, dbemailValue, dbmilesValue, dbhoursValue, dbcostValue);
return null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.final_quote_page, menu);
return true;
}
}
A copy of my complete project can be found at dropboxdotcom/s/4ifg4mu4ynenlim/AdamApp.zip
— modified on Apr 29, 2014, 8:37:57 PM
Recommended editorial content
With your consent, external content is loaded here.
By clicking on the button above, you agree that external content may be displayed to you. Personal data may be transmitted to third-party providers in the process. You can find more information about this in our Privacy Policy.