今天我实现了添加预算和显示预算列表和home页面跳转其他页面的功能。
(1)添加预算
package com.example.feigou.test; import com.example.feigou.activity.MainActivity; import com.example.feigou.db.ComeModle; import com.example.feigou.db.MyDBHelper; import com.example.personmoney.R; import android.app.Activity; import android.content.Intent; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.Spinner; import android.widget.TextView; import android.widget.Toast; public class AddPreActivity extends Activity { private static final String[] style={"现金","银行卡","兑换券"}; private static final String[] fenlei= {"生活用品","工作学习","交通工具","业余爱好","其他"}; private EditText etNum ,etNote; private Spinner spStyle, spFenlei; private TextView tvTime, timeshow; private Button btnRest, btnSubmit; private ArrayAdapter<String> adapterStyle; private ArrayAdapter<String> adapterFenlei; private String money,note,sStyle,sFenlei; String strTime; //数据库 private MyDBHelper DBsqlite; private SQLiteDatabase sqlite; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_addpre); initview(); } public void initview() { btnRest = (Button) findViewById(R.id.income_btn_reset); btnSubmit = (Button) findViewById(R.id.income_btn_submit); // 支出金额 etNum = (EditText) findViewById(R.id.etincome01); // 支出备注 etNote = (EditText) findViewById(R.id.etincomenote); // 重置 btnRest = (Button) findViewById(R.id.income_btn_reset); // 确定 btnSubmit = (Button) findViewById(R.id.income_btn_submit); // 支出时间 tvTime = (TextView) findViewById(R.id.tvincome04); // 显示时间 timeshow = (TextView) findViewById(R.id.tvtime); // 支出方式 spStyle = (Spinner) findViewById(R.id.spinner01); //支出分类 spFenlei = (Spinner) findViewById(R.id.spinner02); // 设置监听是否有输入金额 etNum.addTextChangedListener(new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { // TODO Auto-generated method stub if (s == null || s.length() <= 0) { btnSubmit.setEnabled(false); } else { btnSubmit.setEnabled(true); } } @Override public void afterTextChanged(Editable s) { // TODO Auto-generated method stub } }); // 收入方式适配器添加 adapterStyle = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, style); adapterStyle.setDropDownViewResource(R.layout.myspinner_dropdown); spStyle.setAdapter(adapterStyle); spStyle.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(AddPreActivity.this, "选择的是:" + style[position], Toast.LENGTH_SHORT).show(); sStyle = style[position]; } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); // 收入分类添加适配器 adapterFenlei = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, fenlei); adapterFenlei.setDropDownViewResource(R.layout.myspinner_dropdown); spFenlei.setAdapter(adapterFenlei); spFenlei.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(AddPreActivity.this, "选择的是:" + fenlei[position], Toast.LENGTH_SHORT).show(); sFenlei = fenlei[position]; } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } }); // 重置 btnRest.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub etNum.setText(""); etNote.setText(""); } }); // 确定提交 btnSubmit.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub money = etNum.getText().toString(); note = etNote.getText().toString(); if(new ComeModle(AddPreActivity.this).save("addpre",money,sStyle,sFenlei,strTime,note)){ Toast.makeText(AddPreActivity.this, "保存成功!!", Toast.LENGTH_SHORT).show(); Intent intent = new Intent(AddPreActivity.this,MainActivity.class); startActivity(intent); AddPreActivity.this.finish(); }else{ Toast.makeText(AddPreActivity.this, "保存失败,请重新操作!", Toast.LENGTH_SHORT).show(); } } }); // 输入时间 tvTime.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(AddPreActivity.this, DataActivity.class); startActivityForResult(intent, 0); } }); } // 覆盖onacticityResult方法 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { // TODO Auto-generated method stub // super.onActivityResult(requestCode, resultCode, data); switch (resultCode) { case RESULT_OK: Bundle b = data.getExtras(); strTime = b.getString("time"); timeshow.setText(strTime); break; default: break; } } }
(2)显示预算列表
package com.example.feigou.test; import com.example.feigou.db.MyDBHelper; import com.example.personmoney.R; import android.app.Activity; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.ViewGroup.LayoutParams; import android.widget.TableLayout; import android.widget.TableRow; import android.widget.TextView; public class ListPreActivity extends Activity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_listpre); // 向TableLayout中添加数据库数据 addDates(); } public void addDates() { MyDBHelper database = new MyDBHelper(ListPreActivity.this, "jizhang.db", null, 2);// 这段代码放到Activity类中才用this SQLiteDatabase db = database.getReadableDatabase(); TableLayout table = (TableLayout) findViewById(R.id.tlListCome); //table.setStretchAllColumns(true); // Log.i("test", "1"); Cursor c = db.rawQuery("select * from come where inorout=?", new String[]{"addpre"});// 查询并获得游标 if (c.moveToFirst()) { do { Log.i("c.getcount()=", c.getCount() + ""); String inorout = c.getString(c.getColumnIndex("inorout")); String money = c.getString(c.getColumnIndex("money")); String style = c.getString(c.getColumnIndex("style")); String fenlei = c.getString(c.getColumnIndex("fenlei")); String time = c.getString(c.getColumnIndex("time")); String note = c.getString(c.getColumnIndex("note")); Log.i("inorout:", inorout); Log.i("fenlei:", fenlei); Log.i("time", time); TableRow tablerow = new TableRow(ListPreActivity.this); TextView tvText1 = new TextView(ListPreActivity.this); //汉化 if(inorout.equalsIgnoreCase("income")){ inorout="收入"; }else if(inorout.equalsIgnoreCase("outcome")){ inorout="支出"; }else{ inorout="预算"; } tvText1.setText(inorout); TextView tvText2 = new TextView(ListPreActivity.this); tvText2.setText(money); TextView tvText3 = new TextView(ListPreActivity.this); tvText3.setText(style); TextView tvText4 = new TextView(ListPreActivity.this); tvText4.setText(fenlei); TextView tvText5 = new TextView(ListPreActivity.this); tvText5.setText(time); TextView tvText6 = new TextView(ListPreActivity.this); tvText6.setText(note); tablerow.addView(tvText1); tablerow.addView(tvText2); tablerow.addView(tvText3); tablerow.addView(tvText4); tablerow.addView(tvText5); tablerow.addView(tvText6); table.addView(tablerow, new TableLayout.LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } while (c.moveToNext()); } } }
(3)home页跳转其他页面功能
package com.example.feigou.activity; import com.example.feigou.test.AddPreActivity; import com.example.feigou.test.IncomeActivity; import com.example.feigou.test.ListComeActivity; import com.example.feigou.test.ListPreActivity; import com.example.feigou.test.OutcomeActivity; import com.example.feigou.test.SettingActivity; import com.example.personmoney.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; import android.widget.LinearLayout; public class MainActivity extends Activity implements OnClickListener { private LinearLayout llIncome,llOutcome,llList,llSeting,llModle,llBiaoge,llFenlei; private ImageView iv01; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); initView(); } public void initView() { iv01=(ImageView) findViewById(R.id.ivHomeIncome); llIncome=(LinearLayout) findViewById(R.id.llincome); llOutcome=(LinearLayout) findViewById(R.id.lloutcome); llList=(LinearLayout) findViewById(R.id.lllist); llSeting=(LinearLayout) findViewById(R.id.llsetting); llFenlei=(LinearLayout) findViewById(R.id.llfenlei); llModle=(LinearLayout) findViewById(R.id.llmodle); //设置监听器 llIncome.setOnClickListener(this); llOutcome.setOnClickListener(this); llList.setOnClickListener(this); llSeting.setOnClickListener(this); llFenlei.setOnClickListener(this); llModle.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch(v.getId()) { //判断是否有支出 case R.id.llincome: Log.i("test", "aaa"); Intent intentIncome=new Intent(MainActivity.this,IncomeActivity.class); startActivity(intentIncome); break; case R.id.lloutcome://支出 Intent intentOutcome=new Intent(MainActivity.this,OutcomeActivity.class); startActivity(intentOutcome); break; case R.id.lllist://收支列表 Intent intentList=new Intent(MainActivity.this,ListComeActivity.class); startActivity(intentList); break; case R.id.llsetting://设置列表 Intent intentSetting=new Intent(MainActivity.this,SettingActivity.class); startActivity(intentSetting); break; case R.id.llfenlei://预算列表 Intent intentFenlei=new Intent(MainActivity.this,ListPreActivity.class); startActivity(intentFenlei); break; case R.id.llmodle://收支列表 Intent intentModle=new Intent(MainActivity.this,AddPreActivity.class); startActivity(intentModle); break; default: break; } } }