实例介绍
【实例截图】
【核心代码】
package com.example.sqlite; import java.util.ArrayList; import java.util.Calendar; import java.util.HashMap; import java.util.List; import javax.security.auth.PrivateCredentialPermission; import cn.example.domain.Person; import com.example.service.DBOpenHelper; import com.example.service.PersonService; import android.R.bool; import android.R.integer; import android.os.Bundle; import android.animation.ArgbEvaluator; import android.app.Activity; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.gesture.Prediction; import android.util.Log; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class MainActivity extends Activity { private DBOpenHelper dbOpenHelper; private Person person; private EditText date_edt, week_edt, content_edt, work_hour, month_edt; private ListView show_list; private float hour_workhour = 11.666f; private int year, monthhow, day, weekhow, hour, minute; private PersonService personservice; private TextView show_text1_5,show_text2_0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //对象实例化 personservice=new PersonService(this); dbOpenHelper = new DBOpenHelper(MainActivity.this); dbOpenHelper.getWritableDatabase(); //获取控件的ID date_edt = (EditText) findViewById(R.id.date_edt_id); show_list = (ListView) findViewById(R.id.listview); week_edt = (EditText) findViewById(R.id.week_edt_id); content_edt = (EditText) findViewById(R.id.content_edt_id); work_hour = (EditText) findViewById(R.id.work_hour_edt_id); month_edt = (EditText) findViewById(R.id.month_edt_id); show_text1_5=(TextView)findViewById(R.id.show_text1_5); show_text2_0=(TextView)findViewById(R.id.show_text2_0); // 获取当前时间 Calendar c = Calendar.getInstance(); year = c.get(Calendar.YEAR); monthhow = c.get(Calendar.MONTH); day = c.get(Calendar.DAY_OF_MONTH); weekhow = c.get(Calendar.DAY_OF_WEEK); hour = c.get(Calendar.HOUR_OF_DAY); minute = c.get(Calendar.MINUTE); } // 下面是获取时间按钮事件 public void now_time(View v) { String now_week = null; //把日期与月份给对应的控件 date_edt.setText(String.valueOf(year) "-" String.valueOf(monthhow 1) "-" String.valueOf(day)); month_edt.setText(String.valueOf(monthhow 1)); // 因为每个星期的第一天是从星期天开始的,所以要转换 switch (weekhow) { case 1: now_week="日"; break; case 2: now_week="一"; break; case 3: now_week="二"; break; case 4: now_week="三"; break; case 5: now_week="四"; break; case 6: now_week="五"; break; case 7: now_week="六"; break; default: break; } //把星期给对应的控件 week_edt.setText(now_week); //下面是加班费倍数 if(weekhow==2||weekhow==3||weekhow==4||weekhow==5||weekhow==6) { content_edt.setText("1.5"); } else { content_edt.setText("2"); } Toast.makeText(getApplicationContext(), "今天日期已经填入", 1).show(); } /** * 添加记录并保存进数据库 * * @param person */ public void add(View v) { int n, workhour = 0; boolean enabled = true, a = true, b = true, c = true, d = true, e = true;//标志位 // 获取输入框中的数据 String week = week_edt.getText().toString(); String date = date_edt.getText().toString(); String content = content_edt.getText().toString(); String month = month_edt.getText().toString(); // 先来个判断输入框不能为空 if (week.equals("")) { a = false; Toast.makeText(getApplicationContext(), "星期不能为空", 1).show(); } if (date.equals("")) { b = false; Toast.makeText(getApplicationContext(), "日期不能为空", 1).show(); } if (content.equals("1.5")||content.equals("2")) { c =true; }else { c=false; Toast.makeText(getApplicationContext(), "倍数只能为1.5或2", 1).show(); } if ((work_hour.getText().toString()).equals("")) { d = false; Toast.makeText(getApplicationContext(), "加班时间不能为空", 1).show(); } else { // 为什么要把下面这条件语句放这里,因为如果输入框为空后Integer.valueof()转换,系统就会出错,软件就会自动关闭,所以为空时不让转换。 workhour = Integer.valueOf(work_hour.getText().toString()); } if (month.equals("")) { e = false; Toast.makeText(getApplicationContext(), "月份不能为空", 5).show(); } if (a && b && c && d && e) { // 先来个判断看数据表中已经存在该日期的了,有就不能写入了,只能更改 String now_date = date_edt.getText().toString(); SQLiteDatabase db1 = dbOpenHelper.getReadableDatabase(); // 下面是查找方法,第一个参数为表名,第二个参数为null表示所有的字段,第三个参数为条件,第四个为点位符参数, Cursor cursor = db1.rawQuery("select * from person where date=?", new String[] { now_date.toString() }); for (n = 0; n < cursor.getCount(); n )// 这里的getCount()获取的总记录数量,是指跟上面条件相等值的数量 { if (cursor.moveToNext())// 用for来输出所有相等条件的值 { String date1 = cursor.getString(cursor .getColumnIndex("date")); // 下判断从数据表中能否获取到输入框中的数据,如果有,就不能再次存入了。 if (date1.equals(now_date)) { enabled = false; } } } if (enabled) { // 引用Person类中的构造方法 person = new Person(date, week, content, workhour, month); // 创建数据帮手对象再引用它的方法 SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); ContentValues values = new ContentValues();// 这个是用来存放每个字段的值,第个字段是有相应的值的。 values.put("date", person.getdate());// 括号中,第一个参数为字段名称, values.put("week", person.getweek()); values.put("content", person.getcontent()); values.put("workhour", person.getworkhour()); values.put("month", person.getMonth()); db.insert("person", null, values);// 括号中的第一个参数为表名,第二个参数为空值字段,也就是说如果那个字段的值为空,那就要用到第二个参数, // 例如当amount字段的字为空时,就得这样写db.insert("person","amount",null);,第三个参数就字段相应的值。 if (weekhow == 1 || weekhow == 7) { Toast.makeText(getApplicationContext(),"恭喜你今天赚了" workhour * hour_workhour * 2 "元", 1).show(); } else { Toast.makeText(getApplicationContext(),"恭喜你今天赚了" workhour * hour_workhour * 1.5 "元", 1).show(); } } else { Toast.makeText(getApplicationContext(), "已经记录过了,不能再记录,只能更改", 1) .show(); } } work_hour.setText(""); } public void date_find(View v) { int i; int a, b = 0, c = 0, aa, bb = 0, cc = 0; String time1_5 = "0", time2_0 = "0"; String month = month_edt.getText().toString(); if(!month.equals("")) { String content1_5 = "1.5"; String content2_0 = "2"; SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); // 下面是查找方法,第一个参数为表名,第二个参数为null表示所有的字段,第三个参数为条件,第四个为点位符参数, Cursor cursor = db.rawQuery( "select * from person where month=? and content=?", new String[] { month.toString(), content1_5.toString() }); for (i = 0; i < cursor.getCount(); i )// 这里的getCount()获取的总记录数量,是指跟上面条件相等值的数量 { if (cursor.moveToNext())// 用for来输出所有相等条件的值 { int content = cursor.getInt(cursor.getColumnIndex("content")); int workhour = cursor.getInt(cursor.getColumnIndex("workhour")); String week = cursor.getString(cursor.getColumnIndex("week")); String date = cursor.getString(cursor.getColumnIndex("date")); String month1 = cursor.getString(cursor.getColumnIndex("month")); b = workhour; } a = c b; c = a; time1_5 = String.valueOf(c); } cursor.close(); show_text1_5.setText(month "月份,平常1.5倍的" time1_5 "小时" ",计:" c * 1.5 * hour_workhour "元" "\n" ); // // 下面是查找方法,第一个参数为表名,第二个参数为null表示所有的字段,第三个参数为条件,第四个为点位符参数, Cursor cursor2 = db.rawQuery( "select * from person where month=? and content=?", new String[] { month.toString(), content2_0.toString() }); for (i = 0; i < cursor2.getCount(); i )// 这里的getCount()获取的总记录数量,是指跟上面条件相等值的数量 { if (cursor2.moveToNext())// 用for来输出所有相等条件的值 { int content = cursor2.getInt(cursor2.getColumnIndex("content")); int workhour = cursor2.getInt(cursor2 .getColumnIndex("workhour")); String week = cursor2.getString(cursor2.getColumnIndex("week")); String date = cursor2.getString(cursor2.getColumnIndex("date")); bb = workhour; } aa = cc bb; cc = aa; time2_0 = String.valueOf(cc); } cursor.close();//关闭curson对象 //算出当天记录的时间与金额 show_text2_0.setText(month "月份,周末2倍的" time2_0 "小时" ",计:" cc * 2 * hour_workhour "元" ); }else { Toast.makeText(getApplicationContext(), "统计时月份不能为空", 1).show(); } } // 下面为更改按钮事件 /** * 更新记录 * * @param person */ public void update(View v) { boolean a = true, b = true, c = true, d = true, e = true; int workhour = 0; // 获取输入框中的数据 String week = week_edt.getText().toString(); String date = date_edt.getText().toString(); String content = content_edt.getText().toString(); String month = month_edt.getText().toString(); // 先来个判断输入框是否为空,不空就不能修改成功 if (week.equals("")) { a = false; Toast.makeText(getApplicationContext(), "星期不能为空", 1).show(); } if (date.equals("")) { b = false; Toast.makeText(getApplicationContext(), "日期不能为空", 2).show(); } if (content.equals("1.5")||content.equals("2")) { c =true; }else { c=false; Toast.makeText(getApplicationContext(), "倍数只能为1.5或2", 3).show(); } if ((work_hour.getText().toString()).equals("")) { d = false; Toast.makeText(getApplicationContext(), "加班时间不能为空", 4).show(); } else { // 为什么要把下面这条件语句放这里,因为如果输入框为空后Integer.valueof()转换,系统就会出错,软件就会自动关闭,所以为空时不让转换。 workhour = Integer.valueOf(work_hour.getText().toString()); } if (month.equals("")) { e = false; Toast.makeText(getApplicationContext(), "月份不能为空", 5).show(); } if (a && b && c && d && e) { // 引用Person类中的构造方法 person = new Person(date, week, content, workhour, month); SQLiteDatabase db = dbOpenHelper.getWritableDatabase(); db.execSQL( "update person set month=?,week=?,content=?,workhour=? where date=?", new Object[] { person.getMonth(), person.getweek(), person.getcontent(), person.getworkhour(), person.getdate() }); Toast.makeText(getApplicationContext(), "修改成功", 1).show(); } work_hour.setText(""); } //下面为显示出所有的记录 public void note_content(View v){ SQLiteDatabase db = dbOpenHelper.getReadableDatabase(); //下面是查找方法,第一个参数为表名,第二个参数为null表示所有的字段,第三个参数为条件,第四个为点位符参数, Cursor cursor = db.query("person", null, null, null, null, null, null); int maxlegth=cursor.getCount();//取得数据,总条数 List<Person> persons=personservice.getScrollData(0, maxlegth); List<HashMap<String, Object>> data=new ArrayList<HashMap<String,Object>>(); for(Person person:persons) { HashMap<String, Object> item=new HashMap<String, Object>(); item.put("date", person.getdate()); item.put("week", person.getweek()); item.put("content", person.getcontent()); item.put("workhour", person.getworkhour()); item.put("month", person.getMonth()); data.add(item); } //下面第一个为上下文,第二个为数据集,第三个参数为listvist用的xml文件,后面的就是item对应键,所对应的TextView的ID SimpleAdapter adapter=new SimpleAdapter(this, data, R.layout.data_item, new String[]{"date","week","content","workhour","month"}, new int[]{R.id.date_item,R.id.week_item,R.id.content_item,R.id.work_hour_item,R.id.month_item}); show_list.setAdapter(adapter);//显示出listview } }
标签: 加班时间记录
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论