Java Android应用程序:我的应用程序中有两个表单需要保存到数据库中
第一个表单完全可以保存到数据库中。我试图测试第二个表单是否保存到数据库中,但是,如果选中第一个表单上的复选框,则会出现第二个表单;第二种形式几乎立即出现和消失。有人能帮忙吗 第一种形式的java代码 包com.lifemantsJava Android应用程序:我的应用程序中有两个表单需要保存到数据库中,java,android,database,json,navigation,Java,Android,Database,Json,Navigation,第一个表单完全可以保存到数据库中。我试图测试第二个表单是否保存到数据库中,但是,如果选中第一个表单上的复选框,则会出现第二个表单;第二种形式几乎立即出现和消失。有人能帮忙吗 第一种形式的java代码 包com.lifemants import java.util.ArrayList; import java.util.List; import org.apache.http.NameValuePair; import org.apache.http.messag
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();
}
}
}
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.NameValuePair;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.CheckBox;
导入android.widget.EditText;
导入android.widget.Toast;
公共类MoodTracker扩展活动{
私人对话;
//创建新的url
私有静态字符串url_create_mood=”http://depressionapp.net46.net/depressionapp/moodtracker.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
JSONParser JSONParser=新的JSONParser();
编辑文本自动日期、自动时间、输入地点、输入心情、原因因子、输入因子;
药物治疗;
按钮btnSave;
索氏体;
串索时间;
串到位;
弦乐心境;
串系数;
串输入因子;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.mood_tracker);
autoDate=(EditText)findViewById(R.id.autoDate);
自动时间=(编辑文本)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);
//创建按钮
btnSave=(按钮)findviewbyd(R.id.btnSave);
checkTreatment=(复选框)findViewById(R.id.checkTreatment);
//创建用户
btnSave.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
sAutoDate=autoDate.getText().toString();
sAutoTime=autoTime.getText().toString();
sInputPlace=inputPlace.getText().toString();
sInputMood=inputMood.getText().toString();
sCauseFactor=autoDate.getText().toString();
sInputFactor=inputFactor.getText().toString();
//如果任何字段为空,则显示toast
if(sAutoDate.matches(“”)sInputMood.matches(“”)
sCauseFactor.matches(“”){
Toast.makeText(getApplicationContext(),“请确保所有标有星号的字段都已完成”,Toast.LENGTH_LONG.show();
}否则{
新建CreateMoodTracker().execute();
}
复选框checkTreatment=(复选框)findViewById(R.id.checkTreatment);
if(checkTreatment.isChecked()){
Intent i=新Intent(getApplicationContext(),TreatmentTracker.class);
星触觉(i);
}
}
});
}
/**
*后台异步任务
* */
类CreateMoodTracker扩展了AsyncTask{
成功;
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(MoodTracker.this);
setMessage(“创建跟踪记录…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*创建用户
* */
受保护的字符串doInBackground(字符串…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();
//建筑参数
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“UID”、“1”);
添加参数(新的BasicNameValuePair(“日期”,sAutoDate));
添加参数(新的BasicNameValuePair(“时间”,sAutoTime));
参数添加(新的BasicNameValuePair(“地点”,sInputPlace));
添加参数(新的BasicNameValuePair(“mood”,sInputMood));
参数add(新的BasicNameValuePair(“causefactor”,sCauseFactor));
参数add(新的BasicNameValuePair(“factordetail”,sInputFactor));
//获取JSON对象
//注意,createurl接受POST方法
JSONObject json=jsonParser.makeHttpRequest(url\u create\u mood,
“POST”,params);
//从响应中检查日志cat
d(“创建响应”,json.toString());
//检查成功标签
试一试{
success=json.getInt(TAG_success);
如果(成功==1){
//已成功创建,移动到登录屏幕
Intent i=新的Intent(getApplicationContext(),MainActivity.class);
星触觉(i);
//关闭此屏幕
完成();
}否则{
//创建失败
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//