之前只是效果展示,这里来展示如何实现功能。
首先这个项目是使用sqlite数据库实现访问进行数据表的增删改查功能,系统内置的sqlite数据库内存很小,只能适用于少量数据,大量数据访问还是借助sqlserver、oracle、mysql等数据库借助websevices来操作将结果返回给client,现在只是来实现单一的对一个表的操作,并没有什么特别的用处,学习学习,练练要不就生疏了,以后远程连接数据库的时候可以修改一下对数据库操作的代码,也有很大用处的。
主界面xml文件代码已分享,现在先写相关实体类和数据库创建、对数据增删改查操作及单元测试插入几条数据便于查询。
我都忘了之前主要界面展示啥样了,贴出来看看吧还是:
代码实现我就不介绍了,相信大家也很熟悉了,我是个新手,看看之前代码回顾一下吧,都忘了。如果方便可以看看我写的这个心情日记,挺简单的。
http://blog.csdn.net/luran_fighting/article/details/8176084
1.来说下主程序MainActivity.java
public class MainActivity extends Activity {
private TableLayout table;
private Button select;
EmployeeDao dao = new EmployeeDao(this);
private Button add;
private Button update;
int selectedRow = 0;
int ActivityID=1;
List list = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
table = (TableLayout) this.findViewById(R.id.table);
table.setBackgroundColor(Color.GREEN);
//table.set
add = (Button) this.findViewById(R.id.add);
update = (Button) this.findViewById(R.id.update);
select = (Button) this.findViewById(R.id.select);
// 点击查询按钮处理事件
// Toast.makeText(this, "已查询过了!", Toast.LENGTH_SHORT).show();
select.setOnClickListener(new selectListener());
// 点击添加按钮事件处理,跳转到另一个activity
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(MainActivity.this, AddAndUpdateActivity.class);
Bundle bundle=new Bundle();
ActivityID=1;
bundle.putInt("ID", ActivityID);
i.putExtras(bundle);
startActivity(i);
}
});
// 更新员工信息
update.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent();
i.setClass(MainActivity.this, AddAndUpdateActivity.class);
Bundle bundle=new Bundle();
ActivityID=2;
bundle.putInt("ID", ActivityID);
bundle.putInt("emID", selectedRow);
i.putExtras(bundle);
startActivity(i);
}
});
}
// 查询信息监听类
private class selectListener implements View.OnClickListener {
@Override
public void onClick(View v) {
list = dao.getAll();
if (list.size() != 0) {
for (int i = 0; i
view1.setTextColor(Color.RED);
view1.setPadding(10, 2, 10, 2);//边框左、上、右、下
row.addView(view1);添加一行
TextView view2 = new TextView(MainActivity.this);
view2.setText(em.getName());
view2.setTextSize((float) 18);
view2.setPadding(10, 2, 10, 2);
row.addView(view2);
TextView view3 = new TextView(MainActivity.this);
view3.setText(Integer.toString(em.getAge()));
view3.setTextSize((float) 18);
view3.setGravity(Gravity.CENTER);
view3.setPadding(10, 2, 10, 2);
row.addView(view3);
TextView view4 = new TextView(MainActivity.this);
view4.setText(em.getPosition());
view4.setTextSize((float) 18);
view4.setPadding(10, 2, 10, 2);
row.addView(view4);
TextView view5 = new TextView(MainActivity.this);
view5.setText(em.getDepartment());
view5.setTextSize((float) 18);
view5.setPadding(10, 2, 10, 2);
row.addView(view5);
TextView view6 = new TextView(MainActivity.this);
view6.setText(em.getWorkdate());
view6.setTextSize((float) 18);
view6.setPadding(10, 2, 10, 2);
row.addView(view6);
TextView view7 = new TextView(MainActivity.this);
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
try {
date = format.parse(em.getWorkdate());
} catch (ParseException e) {
e.printStackTrace();
}
float d= (float)((new Date().getTime()-date.getTime())/(24*60*60*1000)/365);//计算工龄
String dd=Integer.toString((int) d+1);
view7.setText(dd);
view7.setTextSize((float) 18);
view7.setPadding(10, 2, 10, 2);
row.addView(view7);
table.addView(row);
row.setOnClickListener(new View.OnClickListener() {//点击某行触发事件
@Override
public void onClick(View v) {
System.out.println("行标记:" + v.getId());
for (int i = 0; i
2.然后是添加和更新的界面,两个功能使用同一个xml文件布局
3.strings.xml文件
4.AddAndUpdateActivity.java
程序可以判定用户是进行添加还是更新,然后进入对应的界面布局和操作。
public class AddAndUpdateActivity extends Activity {
private TextView t; private EditText name;
private EditText age; private EditText position;
private EditText dptmt; private EditText date;
private Button add; private EmployeeDao dao;
private int year; private int month;
private int day; private int DATE_ID = 1;
private int EMPLOYEE_ID; private int ACTIVITY_ID;
// 更新日期
private void setDate() {
date.setText(new StringBuilder().append(year).append("-")
.append((month + 1) <10 ? "0" + (month + 1) : (month + 1))
.append("-").append((day <10) ? "0" + day : day));
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
t = (TextView) this.findViewById(R.id.t);
name = (EditText) this.findViewById(R.id.nm);
age = (EditText) this.findViewById(R.id.ag);
position = (EditText) this.findViewById(R.id.pzs);
dptmt = (EditText) this.findViewById(R.id.dptmt);
date = (EditText) this.findViewById(R.id.wkdt);
add = (Button) this.findViewById(R.id.addnew);
dao = new EmployeeDao(this);
// 创建日历
final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
mOnth= c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
//确认进入的页面是增加还是更新页面
Bundle b = this.getIntent().getExtras();
ACTIVITY_ID = b.getInt("ID");
switch (ACTIVITY_ID) {
case 1:
// 跳转添加页面
setDate();
add.setText(R.string.add);// 改变提示文字
t.setText(R.string.addinfo);
break;
default:
// 更新页面
EMPLOYEE_ID = b.getInt("emID");// 接收上一页捆绑的数据:员工号
add.setText(R.string.update);// 改变提示文字
t.setText(R.string.upinfo);
Employee em = dao.selectByID(EMPLOYEE_ID);
name.setText(em.getName());
age.setText(Integer.toString(em.getAge()));
position.setText(em.getPosition());
dptmt.setText(em.getDepartment());
date.setText(em.getWorkdate());
break;
}
date.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(DATE_ID);//显示对话框
}
});
// 添加记录
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (ACTIVITY_ID) {
case 1:
// 添加员工信息时,获取员工信息保存
Employee employee = new Employee();
employee.setName(name.getText().toString());
employee.setAge(Integer.parseInt(age.getText().toString()));
employee.setPosition(position.getText().toString());
employee.setDepartment(dptmt.getText().toString());
employee.setWorkdate(date.getText().toString());
dao.save(employee);
break;
case 2:
// 确认是更新数据时的处理
Employee emp = new Employee(EMPLOYEE_ID, name.getText()
.toString(), Integer.parseInt(age.getText()
.toString()), position.getText().toString(), dptmt
.getText().toString(), date.getText().toString());
dao.update(emp);
break;
}
// 跳转主页面
Intent i = new Intent();
i.setClass(AddAndUpdateActivity.this, MainActivity.class);
startActivity(i);
}
});
}
// 日期对话框监听器,当设置新的日期时保存当前选定日期 ---- 更新日期,否则为初始出现的日期即现在日期
private DatePickerDialog.OnDateSetListener dateListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int nowyear, int monthOfYear,
int dayOfMonth) {
year = nowyear;
mOnth= monthOfYear;
day = dayOfMonth;
setDate();//显示用日期控件选择的日期在文本框内
}
};
// 重写创建对话框方法:1-日期 2.可以多个其他对话框
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case 1:
return new DatePickerDialog(this, dateListener, year, month, day);
}
return null;
}