package com.bigdo.app; import org.json.JSONObject; import com.bigdo.common.BDSQLiteHelper; import com.bigdo.common.Login; import com.bigdo.common.RConfig; import com.bigdo.common.RState; import com.bigdo.common.User; import com.bigdo.util.WSCheck; import com.bigdo.util.WSHelper; import com.bigdo.util.WSResult; import android.annotation.SuppressLint; import android.content.ContentValues; import android.content.DialogInterface; import android.content.DialogInterface.OnDismissListener; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.text.method.ScrollingMovementMethod; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.EditText; import android.widget.ImageButton; import android.widget.TextView; public class Me_Edit_GrowthHistory_Activity extends BaseActivity { String tag, title, content, createtime; int idgrow = -1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.me_edit_growthhistory_activity); tag = this.getIntent().getStringExtra("tag"); View com_title = (View) this.findViewById(R.id.bd_title_host); TextView bd_title_name = (TextView) com_title .findViewById(R.id.bd_title_name); setInfo(); if ("add".equals(tag)) { bd_title_name.setText("新增"); } else if ("update".equals(tag)) { idgrow = this.getIntent().getIntExtra("idgrow", -1); bd_title_name.setText("编辑"); } TextView bd_title_operate = (TextView) com_title .findViewById(R.id.bd_title_operate); bd_title_operate.setText("保 存"); bd_title_operate.setVisibility(TextView.VISIBLE); bd_title_operate.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { if ("add".equals(tag)) { addGrow(); } else if ("update".equals(tag)) { updteGrow(); } } }); ImageButton bd_title_back = (ImageButton) com_title .findViewById(R.id.bd_title_back); bd_title_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { finish(); } }); EditText grow_content = (EditText) findViewById(R.id.grow_content); grow_content.setMovementMethod(ScrollingMovementMethod.getInstance()); } private void setInfo() { tag = this.getIntent().getStringExtra("tag"); if ("update".equals(tag)) { title = this.getIntent().getStringExtra("title"); content = this.getIntent().getStringExtra("content"); createtime = this.getIntent().getStringExtra("createtime"); } else { title = ""; content = ""; createtime = ""; } EditText t = (EditText) findViewById(R.id.grow_title); t.setText(title); EditText c = (EditText) findViewById(R.id.grow_content); c.setText(content); } private void updteGrow() { if (idgrow >= 0) { User user = Login.get(this); if (user != null) { EditText t = (EditText) findViewById(R.id.grow_title); EditText c = (EditText) findViewById(R.id.grow_content); title = t.getText().toString(); content = c.getText().toString(); if (title == null || title.equals("") || "".equals(title.trim())) { t.setError("标题不能为空"); this.messageBox("标题不能为空"); return; } if (content == null || content.equals("") || "".equals(content.trim())) { t.setError("内容不能为空"); this.messageBox("内容不能为空"); return; } JSONObject jparam = new JSONObject(); try { jparam.put("user", user.user); jparam.put("idgrow", idgrow); jparam.put("title", title); jparam.put("content", content); } catch (Exception e) { this.messageBox("准备更新参数出错."); return; } showProgress("更新中..."); String url = RConfig.updateGrow(this); String data = jparam.toString(); WSHelper ws = new WSHelper(this, "", false, handler, url, "", WSHelper.getStringEntity(data), 2, 0, 0, 0, WSHelper.POST); ws.start(); Log.e("delGrow", url); Log.e("delGrow_data", data); } else { showInfo("请先登录"); } } else { showInfo("成长史数据有误"); } } private void addGrow() { User user = Login.get(this); if (user != null) { EditText t = (EditText) findViewById(R.id.grow_title); EditText c = (EditText) findViewById(R.id.grow_content); title = t.getText().toString(); content = c.getText().toString(); if (title == null || title.equals("") || "".equals(title.trim())) { t.requestFocus(); t.setError("标题不能为空"); showInfo("标题不能为空"); return; } if (content == null || content.equals("") || "".equals(content.trim())) { c.requestFocus(); c.setError("内容不能为空"); showInfo("内容不能为空"); return; } JSONObject jparam = new JSONObject(); try { jparam.put("user", user.user); jparam.put("title", title); jparam.put("content", content); } catch (Exception e) { showInfo("准备请求参数出错."); return; } showProgress("新增中..."); String url = RConfig.addGrow(this); String data = jparam.toString(); WSHelper ws = new WSHelper(this, "", false, handler, url, "", WSHelper.getStringEntity(data), 1, 0, 0, 0, WSHelper.POST); ws.start(); Log.e("delGrow", url); Log.e("delGrow_data", data); } else { showInfo("请先登录"); } } @SuppressLint("HandlerLeak") protected Handler handler = new Handler() { public void handleMessage(Message msg) { super.handleMessage(msg); if (msg.arg1 == RState.WSCall) { hiddenProgress(); WSResult result = (WSResult) msg.getData().getParcelable( WSResult.ResultKey); JSONObject jresult = WSCheck.Json( Me_Edit_GrowthHistory_Activity.this, result); if (result.RequestCode == 1) { addFinsh(jresult); } else if (result.RequestCode == 2) { updateFinsh(jresult); } } } }; private void addFinsh(JSONObject jresult) { if (jresult != null) { try { idgrow = jresult.getInt("idgrow"); createtime = jresult.getString("createtime"); } catch (Exception e) { } if (idgrow >= 0) { updateLocal(); } messageBox("新增成功", new OnDismissListener() { @Override public void onDismiss(DialogInterface arg0) { setResult(1); finish(); } }); } } private void updateFinsh(JSONObject jresult) { if (jresult != null) { updateLocal(); messageBox("更新成功", new OnDismissListener() { @Override public void onDismiss(DialogInterface arg0) { setResult(1); finish(); } }); } } private void updateLocal() { BDSQLiteHelper sqlobj = null; SQLiteDatabase sqlobj_w = null; Cursor cu = null; try { sqlobj = new BDSQLiteHelper(this); sqlobj_w = sqlobj.getWritableDatabase(); ContentValues cv = new ContentValues(); cv.put("idgrow", idgrow); cv.put("title", title); cv.put("content", content); cv.put("createtime", createtime); cu = sqlobj_w.rawQuery("select idgrow from grow where idgrow = ? ", new String[] { idgrow + "" }); int c = cu.getCount(); try { if (cu != null) { cu.close(); } } catch (Exception ex) { } cu = null; if (c <= 0) { sqlobj_w.insert("grow", "", cv); } else { cv.remove("idgrow"); c = sqlobj_w.update("grow", cv, "idgrow = ?", new String[] { idgrow + "" }); } cv = null; } catch (Exception e) { showInfo("缓存成长史数据出错."); } try { if (cu != null) { cu.close(); cu = null; } } catch (Exception ex) { } try { if (sqlobj_w != null) { sqlobj_w.close(); sqlobj_w = null; } } catch (Exception ex) { } try { if (sqlobj != null) { sqlobj.close(); sqlobj = null; } } catch (Exception ex) { } } @Override public void onClearData() { // TODO Auto-generated method stub } @Override public void onVideoBroadcast(Intent intent) { // TODO Auto-generated method stub } }