第一种形式将保存到数据库完全罚款。我正在尝试测试第二个表单是否保存到数据库中,但是,如果在第一个表单上选中了复选框,则将出现第二种形式。第二种形式出现并几乎立即消失。有人可以帮忙吗?
首先形式的Java代码
软件包com.lifematters;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;
public class MoodTracker extends Activity {
private ProgressDialog pDialog;
// url to create new
private static String url_create_mood = "http://depressionapp.net46.net/depressionapp/moodtracker.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
JSONParser jsonParser = new JSONParser();
EditText autoDate, autoTime, inputPlace, inputMood, causeFactor, inputFactor;
CheckBox checkTreatment;
Button btnSave;
String sAutoDate;
String sAutoTime;
String sInputPlace;
String sInputMood;
String sCauseFactor;
String sInputFactor;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mood_tracker);
autoDate = (EditText) findViewById(R.id.autoDate);
autoTime = (EditText) findViewById(R.id.autoTime);
inputPlace = (EditText) findViewById(R.id.inputPlace);
inputMood = (EditText) findViewById(R.id.inputMood);
causeFactor = (EditText) findViewById(R.id.chooseFactor);
inputFactor = (EditText) findViewById(R.id.inputFactor);
// Create button
btnSave = (Button) findViewById(R.id.btnSave);
checkTreatment = (CheckBox) findViewById(R.id.checkTreatment);
// create user
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sAutoDate = autoDate.getText().toString();
sAutoTime = autoTime.getText().toString();
sInputPlace = inputPlace.getText().toString();
sInputMood = inputMood.getText().toString();
sCauseFactor = autoDate.getText().toString();
sInputFactor = inputFactor.getText().toString();
//if any field is empty, display toast
if (sAutoDate.matches("") || sInputMood.matches("") ||
sCauseFactor.matches("")) {
Toast.makeText(getApplicationContext(), "Please ensure all fields marked with asterisks are completed", Toast.LENGTH_LONG).show();
} else {
new CreateMoodTracker().execute();
}
CheckBox checkTreatment = (CheckBox ) findViewById(R.id.checkTreatment);
if ( checkTreatment.isChecked() ) {
Intent i = new Intent(getApplicationContext(), TreatmentTracker.class);
startActivity(i);
}
}
});
}
/**
* Background Asynchronous Task
* */
class CreateMoodTracker extends AsyncTask<String, String, String> {
int success;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MoodTracker.this);
pDialog.setMessage("Creating Tracker Record...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Creating user
* */
protected String doInBackground(String... args) {
sAutoDate = autoDate.getText().toString();
sAutoTime = autoTime.getText().toString();
sInputPlace = inputPlace.getText().toString();
sInputMood = inputMood.getText().toString();
sCauseFactor = causeFactor.getText().toString();
sInputFactor = inputFactor.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("UID", "1"));
params.add(new BasicNameValuePair("date", sAutoDate));
params.add(new BasicNameValuePair("time", sAutoTime));
params.add(new BasicNameValuePair("place", sInputPlace));
params.add(new BasicNameValuePair("mood", sInputMood));
params.add(new BasicNameValuePair("causefactor", sCauseFactor));
params.add(new BasicNameValuePair("factordetail", sInputFactor));
// getting JSON Object
// Note that create URL accepts POST method
JSONObject json = jsonParser.makeHttpRequest(url_create_mood,
"POST", params);
// check log cat from response
Log.d("Create Response", json.toString());
// check for success tag
try {
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully created , move to login screen
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
// closing this screen
finish();
} else {
// failed to create
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once done
pDialog.dismiss();
}
}
}
将复选框的值从第一个形式发送到第二个形式,无论复选框是true还是False。然后控制值。