实例介绍
【实例简介】
【实例截图】
【核心代码】
public class TransactionTabActivity extends Activity implements OnClickListener,OnCheckedChangeListener,OnItemSelectedListener{ final static int INCOME_MODE = 0; final static int PAYOUT_MODE = 1; final static int EDIT_MODE = 2; private int type = PAYOUT_MODE;//操作类型0收入、1支出、2编辑 private String[] str = null; private String[] accountId = null; private MyDbHelper db = null; CommonData commondata = CommonData.getInstance(); private Calendar calendar = Calendar.getInstance(); private DatePickerDialog datePicker = null; private AlertDialog dialog = null; private ArrayAdapter<String> adapter; private List<String> list = null; private TextView title_tv = null; private RadioGroup trans_type_tab_rg = null; private RadioButton rb1=null; private RadioButton rb2=null; private FrameLayout corporation_fl = null; private FrameLayout empty_fl = null; private Button cost_btn = null; private String value="0"; private Spinner first_level_category_spn = null; private Spinner sub_category_spn = null; private int type_sub_id = 0; private Spinner account_spn = null; private Spinner corporation_spn = null; private Button trade_time_btn = null; private Spinner project_spn = null; private Button memo_btn = null; private Button save_btn = null; private Button cancel_btn = null; private EditText edit = null; TransactionData data; private int isInitOnly = 0; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.add_or_edit_expense_activity); str = new String[]{getString(R.string.edit_in),getString(R.string.edit_out)}; Intent intent = getIntent(); type = intent.getIntExtra("mode", 1); data = (TransactionData)getIntent().getParcelableExtra("data"); loadingFormation(); initData(); } private void loadingFormation(){ title_tv = (TextView) findViewById(R.id.title_tv); trans_type_tab_rg = (RadioGroup) findViewById(R.id.trans_type_tab_rg); rb1 = (RadioButton) findViewById(R.id.payout_tab_rb); rb1.setChecked(true); rb1.setOnCheckedChangeListener(this); cost_btn=(Button)findViewById(R.id.cost_btn); cost_btn.setOnClickListener(this); corporation_fl=(FrameLayout)findViewById(R.id.corporation_fl); empty_fl=(FrameLayout)findViewById(R.id.empty_fl); first_level_category_spn = (Spinner) findViewById(R.id.first_level_category_spn); first_level_category_spn.setOnItemSelectedListener(this); sub_category_spn = (Spinner) findViewById(R.id.sub_category_spn); sub_category_spn.setOnItemSelectedListener(this); account_spn = (Spinner) findViewById(R.id.account_spn); corporation_spn = (Spinner) findViewById(R.id.corporation_spn); trade_time_btn = (Button) findViewById(R.id.trade_time_btn); trade_time_btn.setOnClickListener(this); project_spn = (Spinner) findViewById(R.id.project_spn); memo_btn = (Button) findViewById(R.id.memo_btn); memo_btn.setOnClickListener(this); save_btn = (Button) findViewById(R.id.save_btn); save_btn.setOnClickListener(this); cancel_btn = (Button) findViewById(R.id.cancel_btn); cancel_btn.setOnClickListener(this); } private void initData(){ db = SplashScreenActivity.db; if(type == EDIT_MODE){ //支出 if(data.type == 1) { corporation_fl.setVisibility(View.VISIBLE); empty_fl.setVisibility(View.GONE); }else //收入 { corporation_fl.setVisibility(View.GONE); empty_fl.setVisibility(View.VISIBLE); } trans_type_tab_rg.setVisibility(View.GONE); title_tv.setVisibility(View.VISIBLE); title_tv.setText(str[data.type]); cost_btn.setText(DecimalFormat.getCurrencyInstance().format(data.money)); value = String.valueOf(String.format("%.2f", data.money)); trade_time_btn.setText(data.date); calendar.set(Integer.valueOf(data.date.substring(0, 4)), Integer.valueOf(data.date.substring(5, 7))-1, Integer.valueOf(data.date.substring(8, 10))); memo_btn.setText(data.memo); }else{ trade_time_btn.setText(format(calendar.getTime())); } corporation_spn.setAdapter(addapter(list,7)); project_spn.setAdapter(addapter(list,8)); updateInfo(-1); } /**更新类别数据*/ private void updateInfo(int position){ type_sub_id = 0; int id = 0; int id_sub = 0; String strWhere = ""; int account_num = 0; switch (type) { case PAYOUT_MODE: id = 0; id_sub = 1; strWhere = "<>1"; break; case INCOME_MODE: id = 2; id_sub = 3; strWhere = "=0"; break; default: if(data.type == INCOME_MODE){ id = 2; id_sub = 3; strWhere = "=0"; }else{ id = 0; id_sub = 1; strWhere = "<>1"; } break; } Cursor cursor = null; if(position < 0){ list = new ArrayList<String>(); cursor = db.select(MyDbInfo.getTableNames()[6], MyDbInfo.getFieldNames()[6], "(select POSTIVE from TBL_ACCOUNT_TYPE where ID=" MyDbInfo.getFieldNames()[6][2] ")" strWhere, null, null, null, null); accountId = new String[cursor.getCount()]; while (cursor.moveToNext()) { list.add(cursor.getString(1)); accountId[account_num] = cursor.getString(0); account_num ; } adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); account_spn.setAdapter(adapter); first_level_category_spn.setAdapter(addapter(list,id)); position = 0; } list = new ArrayList<String>(); cursor = db.select(MyDbInfo.getTableNames()[id_sub], MyDbInfo.getFieldNames()[id_sub], MyDbInfo.getFieldNames()[id_sub][2] "=?", new String[]{String.valueOf(position 1)}, null, null, null); while (cursor.moveToNext()) { if(type_sub_id == 0) type_sub_id = cursor.getInt(0); list.add(cursor.getString(1)); } adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sub_category_spn.setAdapter(adapter); cursor.close(); } private void saveInfo(){ if(value.equals("") || value == null || Double.parseDouble(value) <= 0){ Toast.makeText(getApplicationContext(), getString(R.string.input_message), Toast.LENGTH_SHORT).show(); return; } int tableId = 0; String fieldNames[] = null; String values[] = null; switch (type) { case PAYOUT_MODE: tableId = 9; fieldNames = new String[]{"AMOUNT", "EXPENDITURE_CATEGORY_ID", "EXPENDITURE_SUB_CATEGORY_ID", "ACCOUNT_ID", "STORE_ID", "ITEM_ID", "DATE", "MEMO"}; values = new String[]{ value, String.valueOf(first_level_category_spn.getSelectedItemPosition() 1), String.valueOf(sub_category_spn.getSelectedItemPosition() type_sub_id), accountId[account_spn.getSelectedItemPosition()], String.valueOf(corporation_spn.getSelectedItemPosition() 1), String.valueOf(project_spn.getSelectedItemPosition() 1), trade_time_btn.getText().toString(), memo_btn.getText().toString() }; updataAccount(type); break; case INCOME_MODE: tableId = 10; fieldNames = new String[]{"AMOUNT", "INCOME_CATEGORY_ID", "INCOME_SUB_CATEGORY_ID", "ACCOUNT_ID", "ITEM_ID", "DATE", "MEMO"}; values = new String[]{ value, String.valueOf(first_level_category_spn.getSelectedItemPosition() 1), String.valueOf(sub_category_spn.getSelectedItemPosition() type_sub_id), accountId[account_spn.getSelectedItemPosition()], String.valueOf(project_spn.getSelectedItemPosition() 1), trade_time_btn.getText().toString(), memo_btn.getText().toString() }; updataAccount(type); break; default: if(data.type == PAYOUT_MODE){ tableId = 9; fieldNames = new String[]{"AMOUNT", "EXPENDITURE_CATEGORY_ID", "EXPENDITURE_SUB_CATEGORY_ID", "ACCOUNT_ID", "STORE_ID", "ITEM_ID", "DATE", "MEMO"}; values = new String[]{ value, String.valueOf(first_level_category_spn.getSelectedItemPosition() 1), String.valueOf(sub_category_spn.getSelectedItemPosition() type_sub_id), accountId[account_spn.getSelectedItemPosition()], String.valueOf(corporation_spn.getSelectedItemPosition() 1), String.valueOf(project_spn.getSelectedItemPosition() 1), trade_time_btn.getText().toString(), memo_btn.getText().toString() }; updataAccount(data.type); }else if(data.type == INCOME_MODE){ tableId = 10; fieldNames = new String[]{"AMOUNT", "INCOME_CATEGORY_ID", "INCOME_SUB_CATEGORY_ID", "ACCOUNT_ID", "ITEM_ID", "DATE", "MEMO"}; values = new String[]{ value, String.valueOf(first_level_category_spn.getSelectedItemPosition() 1), String.valueOf(sub_category_spn.getSelectedItemPosition() type_sub_id), accountId[account_spn.getSelectedItemPosition()], String.valueOf(project_spn.getSelectedItemPosition() 1), trade_time_btn.getText().toString(), memo_btn.getText().toString() }; updataAccount(data.type); } break; } if(type == EDIT_MODE){ db.update(MyDbInfo.getTableNames()[tableId], fieldNames, values, "ID=" data.infoId, null); Toast.makeText(getApplicationContext(), getString(R.string.edit_message),Toast.LENGTH_SHORT).show(); }else{ db.insert(MyDbInfo.getTableNames()[tableId], fieldNames,values); Toast.makeText(getApplicationContext(), getString(R.string.save_message),Toast.LENGTH_SHORT).show(); } exit(); } private void updataAccount(int type){ Iterator<AccountData> iteratorSort = commondata.account.values().iterator(); while (iteratorSort.hasNext()){ AccountData data = iteratorSort.next(); if(data.id == Integer.parseInt(accountId[account_spn.getSelectedItemPosition()])) { if(type == INCOME_MODE){ data.balance = data.balance Double.parseDouble(value); commondata.updateAccount(data); }else if(type == PAYOUT_MODE){ data.balance = data.balance-Double.parseDouble(value); commondata.updateAccount(data); } return; } } } private ArrayAdapter<String> addapter(List<String> list,int id){ list = new ArrayList<String>(); Cursor cursor = db.select(MyDbInfo.getTableNames()[id], MyDbInfo.getFieldNames()[id], null, null, null, null, null); while (cursor.moveToNext()) { list.add(cursor.getString(1)); } adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); cursor.close(); return adapter; } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub if (resultCode == Activity.RESULT_OK && requestCode == 0) { Bundle extras = data.getExtras(); value = extras.getString("value"); cost_btn.setText(DecimalFormat.getCurrencyInstance().format(Double.parseDouble(value))); } } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub //支出 if(rb1.isChecked()) { type = PAYOUT_MODE; corporation_fl.setVisibility(View.VISIBLE); empty_fl.setVisibility(View.GONE); }else //收入 { type = INCOME_MODE; corporation_fl.setVisibility(View.GONE); empty_fl.setVisibility(View.VISIBLE); } updateInfo(-1); } @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub if(view == first_level_category_spn.getSelectedView()){//类别更新 if(type == EDIT_MODE && isInitOnly == 0){ isInitOnly = 1; first_level_category_spn.setSelection(data.category_id-1); account_spn.setSelection(data.account_id-1); corporation_spn.setSelection(data.shop_id-1); project_spn.setSelection(data.item_id-1); updateInfo(data.category_id-1); sub_category_spn.setSelection(data.subcategory_id-type_sub_id); }else{ if(isInitOnly==1){ isInitOnly = 2; return; } updateInfo(position); } } } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } @Override public void onClick(View v) { // TODO Auto-generated method stub if(v == cost_btn){ Intent i=new Intent(TransactionTabActivity.this,KeyPad.class); i.putExtra("value", value); startActivityForResult(i, 0); } if(v == trade_time_btn){ datePicker = new DatePickerDialog(this, mDateSetListenerSatrt, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_MONTH)); datePicker.show(); } if(v == memo_btn){ edit = new EditText(this); edit.setLines(5); edit.setText(memo_btn.getText()); dialog = new AlertDialog.Builder(this) .setTitle(getString(R.string.dialog_memo_title)) .setView(edit) .setPositiveButton(getString(R.string.dialog_memo_ok), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub memo_btn.setText(edit.getText()); } }).setNegativeButton(getString(R.string.dialog_memo_cancle), new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub } }).show(); } if(v == save_btn){ saveInfo(); } if(v == cancel_btn){ exit(); } } public void onBackPressed() { exit(); } private void exit(){ if(type != EDIT_MODE){ Intent intent = new Intent(TransactionTabActivity.this,MainActivity.class); startActivity(intent); finish(); }else{ this.setResult(RESULT_OK, getIntent()); this.finish(); } } private DatePickerDialog.OnDateSetListener mDateSetListenerSatrt = new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { calendar.set(Calendar.DAY_OF_MONTH, dayOfMonth); calendar.set(Calendar.MONTH, monthOfYear); calendar.set(Calendar.YEAR, year); trade_time_btn.setText(format(calendar.getTime())); } }; private String format(Date date){ String str = ""; SimpleDateFormat ymd = null; ymd = new SimpleDateFormat("yyyy-MM-dd"); str = ymd.format(date); return str; } }
好例子网口号:伸出你的我的手 — 分享!
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论