PageRenderTime 31ms CodeModel.GetById 12ms app.highlight 15ms RepoModel.GetById 1ms app.codeStats 0ms

/src/com/charmmy/activity/ContactEditActivity.java

http://book-address.googlecode.com/
Java | 153 lines | 125 code | 24 blank | 4 comment | 11 complexity | 560f1896fe1fb71ba49c0aaec779b757 MD5 | raw file
  1package com.charmmy.activity;
  2
  3import android.app.Activity;
  4import android.content.Intent;
  5import android.os.Bundle;
  6import android.view.Gravity;
  7import android.view.KeyEvent;
  8import android.view.View;
  9import android.view.Window;
 10import android.widget.Button;
 11import android.widget.EditText;
 12import android.widget.TextView;
 13import android.widget.Toast;
 14
 15import com.charmmy.dao.PeopleDao;
 16import com.charmmy.pojo.People;
 17import com.charmmy.tools.DefineFinal;
 18
 19/*
 20 * James Liu
 21 * ?????
 22 */
 23public class ContactEditActivity extends Activity {
 24	 
 25	private int id;
 26	private String name;
 27	private String phone;
 28	private String tel; 
 29	private String email; 
 30	private String address;
 31	private String backContent;
 32	
 33	private EditText nameEdit;
 34	private EditText phoneEdit;
 35	private EditText telEdit;
 36	private EditText emailEdit;
 37	private EditText addressEdit;
 38	private EditText backContentEdit;
 39	
 40	private boolean isEdit = false;
 41	
 42	private Intent intent;
 43	
 44	@Override
 45    public void onCreate(Bundle savedInstanceState) {
 46		 super.onCreate(savedInstanceState);
 47		 requestWindowFeature(Window.FEATURE_NO_TITLE);
 48		 requestWindowFeature(Window.FEATURE_PROGRESS);
 49	     setContentView(R.layout.contact_edit);
 50	     initValues();
 51	     
 52	     intent = new Intent();
 53		 intent.setClass(ContactEditActivity.this, MainTabActivity.class);
 54			
 55	     Button saveBtn = (Button) findViewById(R.id.btnSave);
 56	     saveBtn.setOnClickListener(new View.OnClickListener() {
 57			
 58			@Override
 59			public void onClick(View v) {
 60				getString();
 61				if(false == checkEditValues()) {
 62					return;
 63				}
 64				
 65				People people = 
 66					new People(id,name, phone, 
 67							tel, email, address, backContent);
 68				PeopleDao dao = new PeopleDao(ContactEditActivity.this);
 69				if(isEdit == false) {
 70					dao.add(people);
 71				} else {
 72					dao.update(people);
 73				}
 74				
 75				Toast.makeText(ContactEditActivity.this, "???????", Toast.LENGTH_SHORT).show();
 76				startActivity(intent);
 77				finish();
 78			}
 79		});
 80	     
 81	    Button cancelBtn = (Button) findViewById(R.id.btnCancel);
 82	    cancelBtn.setOnClickListener(new View.OnClickListener() {
 83			
 84			@Override
 85			public void onClick(View v) {
 86				startActivity(intent);
 87				finish();
 88			}
 89		});
 90	}
 91	
 92	private void initValues() {
 93		nameEdit = (EditText) findViewById(R.id.nameEdit);
 94		phoneEdit = (EditText) findViewById(R.id.phoneEdit);
 95		telEdit = (EditText) findViewById(R.id.telEdit);
 96		emailEdit = (EditText) findViewById(R.id.emailEdit);
 97		addressEdit = (EditText) findViewById(R.id.addressEdit);
 98		backContentEdit = (EditText) findViewById(R.id.backContentEdit);
 99		TextView contactEditTitle = (TextView) findViewById(R.id.contactEditTitle);
100		
101		People mPeople = (People)getIntent().getSerializableExtra(DefineFinal.INTENT_PEOPLE_DATA);
102	    if(mPeople != null) {
103	    	 isEdit = true;
104	    	 contactEditTitle.setText("?????");
105	    	 id = mPeople.getId();
106	    	 nameEdit.setText(mPeople.getName());
107	    	 phoneEdit.setText(mPeople.getPhone());
108	    	 telEdit.setText(mPeople.getTel());
109	    	 emailEdit.setText(mPeople.getEmail());
110	    	 addressEdit.setText(mPeople.getAddress());
111	    	 backContentEdit.setText(mPeople.getBackContent());
112	    }
113	}
114	
115	private void getString() {
116		name = nameEdit.getText().toString();
117		phone = phoneEdit.getText().toString();
118		tel = telEdit.getText().toString();
119		email = emailEdit.getText().toString();
120		address = addressEdit.getText().toString();
121		backContent = backContentEdit.getText().toString();
122	}
123	private boolean checkEditValues() {
124		if("".equals(name)) {
125			showToast(R.string.contact_edit_name_edit_null);
126			return false;
127		}
128		return true;
129	}
130	
131	private void showToast(int errInfoId) {
132		Toast toast = Toast.makeText(ContactEditActivity.this, errInfoId, Toast.LENGTH_SHORT);
133		toast.setGravity(Gravity.CENTER, 0, 0);
134		toast.show();
135	}
136
137	@Override
138	protected void onStop() {
139		super.onDestroy();
140	}
141	
142	@Override
143	public boolean onKeyDown(int keyCode,KeyEvent event){   
144		 if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {   
145			 startActivity(intent);
146			 finish();
147		 }
148		 return false;
149	}
150	
151	
152
153}