You are on page 1of 5

package origineit.test.

jobform;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
public class skills extends Activity {
/** For convenience purpose, the Button ID will be stored
* to database instead of skill name. */
private float oldTouchValue;
private FormDbAdapter mDbHelper;
private Long mRowId;
private Button cSharp, cLang, visualC, java, vb, perl, win, unix;
private Button linux, php, oracle, mysql, mssql;
private ImageButton nextButton, previousButton;
private LinearLayout skillsView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
mDbHelper = new FormDbAdapter(this);
mDbHelper.open();
setContentView(R.layout.skills);
cSharp = (Button)findViewById(R.id.cSharp);
cLang = (Button)findViewById(R.id.cLang);
visualC = (Button)findViewById(R.id.visualC);
java = (Button)findViewById(R.id.java);
vb = (Button)findViewById(R.id.vb);
perl = (Button)findViewById(R.id.perl);
win = (Button)findViewById(R.id.win);
unix = (Button)findViewById(R.id.unix);
linux = (Button)findViewById(R.id.linux);
php = (Button)findViewById(R.id.php);
oracle = (Button)findViewById(R.id.oracle);
mysql = (Button)findViewById(R.id.mysql);
mssql = (Button)findViewById(R.id.mssql);
nextButton = (ImageButton)findViewById(R.id.skillsNext);
previousButton = (ImageButton)findViewById(R.id.skillsPrevious);
skillsView = (LinearLayout)findViewById(R.id.skillsView);
mRowId = (savedInstanceState == null)? null:
(Long)savedInstanceState.getSerializable(FormDbAdapter.K
EY_ID);
if(mRowId == null){
Bundle extras = getIntent().getExtras();
mRowId = extras.getLong(FormDbAdapter.KEY_ID);
}
populateFields();
nextButton.setOnClickListener(onClick);
previousButton.setOnClickListener(onClick);
cSharp.setOnClickListener(onClick);
cLang.setOnClickListener(onClick);
visualC.setOnClickListener(onClick);
java.setOnClickListener(onClick);
vb.setOnClickListener(onClick);
perl.setOnClickListener(onClick);
win.setOnClickListener(onClick);
unix.setOnClickListener(onClick);
linux.setOnClickListener(onClick);
php.setOnClickListener(onClick);
oracle.setOnClickListener(onClick);
mysql.setOnClickListener(onClick);
mssql.setOnClickListener(onClick);
skillsView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
oldTouchValue = event.getX();
break;
case MotionEvent.ACTION_UP:
if(event.getX() - oldTouchValue < -200){
createIntent(true);
return true;
}else if(event.getX() - oldTouchValue >
200){
createIntent(false);
return true;
}
}
return false;
}
});
}
protected View.OnClickListener onClick = new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.skillsNext:
createIntent(true);
break;
case R.id.skillsPrevious:
createIntent(false);
break;
default:
addSkillDialog(v.getId());
}
}
};
/** direction = true => next page
* direction = false => previous page*/
protected void createIntent(boolean direction){
if(direction){
Intent i = new Intent();
i.putExtra(FormDbAdapter.KEY_ID, mRowId);
i.setClass(skills.this, languages.class);
startActivityForResult(i, 0);
}else{
setResult(RESULT_OK);
finish();
}
}
/** Create custom dialog for user to input skills info.
* @param id: id of the button clicked by user. */
protected void addSkillDialog(int id) {
// TODO Auto-generated method stub
final Button button = (Button)findViewById(id);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
AlertDialog alertDialog;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(
LAYOUT_INFLATER_SERVICE);
final View layout = inflater.inflate(R.layout.skillsdialog,
(ViewGroup)findViewById(R.id.skillsDialogId));
builder.setView(layout);
final RadioGroup radioExp = (RadioGroup)layout.findViewById(R.id
.radioExp);
final RadioGroup radioPro = (RadioGroup)layout.findViewById(R.id
.radioPro);
Cursor c = mDbHelper.fetchSkills(mRowId, button.getId());
if(c.moveToFirst()){
startManagingCursor(c);
if(c.getString(c.getColumnIndexOrThrow(FormDbAdapter.KEY
_SKILL_YEARS)).equals("0")){
radioExp.check(R.id.zeroYear);
}else if (c.getString(c.getColumnIndexOrThrow(FormDbAdap
ter.KEY_SKILL_YEARS)).equals("1-2")){
radioExp.check(R.id.onetotwo);
}else if (c.getString(c.getColumnIndexOrThrow(FormDbAdap
ter.KEY_SKILL_YEARS)).equals("3-4")){
radioExp.check(R.id.threetofour);
}else{
radioExp.check(R.id.five);
}
if(c.getString(c.getColumnIndexOrThrow(FormDbAdapter.KEY
_SKILL_PRO)).equals("B")){
radioPro.check(R.id.B);
}else if(c.getString(c.getColumnIndexOrThrow(FormDbAdapt
er.KEY_SKILL_PRO)).equals("I")){
radioPro.check(R.id.I);
}else{
radioPro.check(R.id.A);
}
}
builder.setTitle(button.getText().toString())
.setPositiveButton(R.string.ok, new DialogInterface.OnClickListe
ner() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String years = null;
String pro = null;
int expId = radioExp.getCheckedRadioButtonId();
int proId = radioPro.getCheckedRadioButtonId();
switch(expId){
case R.id.zeroYear:
years = "0";
break;
case R.id.onetotwo:
years = "1-2";
break;
case R.id.threetofour:
years = "3-4";
break;
case R.id.five:
years = ">5";
}
switch(proId){
case R.id.B:
pro = "B";
break;
case R.id.I:
pro = "I";
break;
case R.id.A:
pro = "A";
}
mDbHelper.newSkill(mRowId, button.getId(), pro,
years);
button.setTypeface(Typeface.DEFAULT_BOLD);
}
})
.setNeutralButton("Undo Selection", new DialogInterface.OnClickL
istener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
mDbHelper.deleteSkill(mRowId, button.getId());
button.setTypeface(Typeface.DEFAULT);
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickL
istener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
alertDialog = builder.create();
alertDialog.show();
}
/** Populate the fields in View if existed in database. */
protected void populateFields(){
Cursor c = mDbHelper.fetchAllSkills(mRowId);
while (c.moveToNext()){
startManagingCursor(c);
Button skillBtn = (Button)findViewById(c.getInt(c.getCol
umnIndexOrThrow(FormDbAdapter.KEY_SKILL_NAME)));
skillBtn.setTypeface(Typeface.DEFAULT_BOLD);
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (resultCode){
case MainPage.ACTIVITY_BACK:
populateFields();
break;
case MainPage.ACTIVITY_DONE:
setResult(MainPage.ACTIVITY_DONE);
finish();
}
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
populateFields();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
super.onSaveInstanceState(outState);
outState.putSerializable(FormDbAdapter.KEY_ID, mRowId);
}
}

You might also like