本文整理自网络,侵删。
百度上流传最广的版本有所欠缺,并不能直接使用,同时有很多不必要的功能,这是我进行删减、修改、核查后的版本,根据下面的步骤一步步来直接能够运行程序。
本程序实现的功能是增删改查以及全选
首先是程序提纲
主要部分是java文件和xml文件。
activity放在java文件里面,xml文件就是布局文件,用来规定界面的显示格式。
类定义的Java文件
StudentDao
StudnetDBHelper
Student
TableContanst
其他文件
string .xml
color.xml
styles.xml
AndroidManifest.xml(自定义的活动需要手动添加到此文件中)
下面看看我的文件目录
值得注意的是,menu.xml不是放在layout目录下,而是放在menu目录下。
然后依次介绍各个activity的代码
主界面是StudentListActivity
代码如下
java文件:StudentListActivity
package com.example.asus.student; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import android.app.AlertDialog; import android.app.ListActivity; import android.content.DialogInterface; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.CheckBox; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.SimpleCursorAdapter; import android.widget.Toast; import StudentDBHelper.StudentDBHelper; import Student.Student; //import AddStudentActivity; import TableContanst.TableContanst; public class StudentListActivity extends ListActivity implements OnClickListener, OnItemClickListener, OnItemLongClickListener { private static final String TAG = "TestSQLite"; private Button addStudent; private Cursor cursor; private SimpleCursorAdapter adapter; private ListView listView; private List<Long> list; private RelativeLayout relativeLayout; private Button searchButton; private Button selectButton; private Button deleteButton; private Button selectAllButton; private Button canleButton; private LinearLayout layout; private StudentDao dao; private Student student; private Boolean isDeleteList = false; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Log.e(TAG, "onCreate"); list = new ArrayList<Long>(); student = new Student(); dao = new StudentDao(new StudentDBHelper(this)); addStudent = (Button) findViewById(R.id.btn_add_student); searchButton = (Button) findViewById(R.id.bn_search_id); selectButton = (Button) findViewById(R.id.bn_select); deleteButton = (Button) findViewById(R.id.bn_delete); selectAllButton = (Button) findViewById(R.id.bn_selectall); canleButton = (Button) findViewById(R.id.bn_canel); layout = (LinearLayout) findViewById(R.id.showLiner); relativeLayout=(RelativeLayout) findViewById(R.id.RelativeLayout); listView = getListView(); // 为按键设置监听 addStudent.setOnClickListener(this); searchButton.setOnClickListener(this); selectButton.setOnClickListener(this); deleteButton.setOnClickListener(this); canleButton.setOnClickListener(this); selectAllButton.setOnClickListener(this); listView.setOnItemClickListener(this); listView.setOnItemLongClickListener(this); listView.setOnCreateContextMenuListener(this); } // 调用load()方法将数据库中的所有记录显示在当前页面 @Override protected void onStart() { super.onStart(); load(); } public void onClick(View v) { // 跳转到添加信息的界面 if (v == addStudent) { startActivity(new Intent(StudentListActivity.this, AddStudentActivity.class)); } else if (v == searchButton) { // 跳转到查询界面 startActivity(new Intent(this, StudentSearch.class)); } else if (v == selectButton) { // 跳转到选择界面 isDeleteList = !isDeleteList; if (isDeleteList) { checkOrClearAllCheckboxs(true); } else { showOrHiddenCheckBoxs(false); } } else if (v == deleteButton) { // 删除数据 if (list.size() > 0) { for (int i = 0; i < list.size(); i++) { long id = list.get(i); Log.e(TAG, "delete id=" + id); int count = dao.deleteStudentById(id); } dao.closeDB(); load(); } } else if (v == canleButton) { // 点击取消,回到初始界面 load(); layout.setVisibility(View.GONE); isDeleteList = !isDeleteList; } else if (v == selectAllButton) { // 全选,如果当前全选按钮显示是全选,则在点击后变为取消全选,如果当前为取消全选,则在点击后变为全选 selectAllMethods(); } } // 创建菜单 public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { MenuInflater inflater = new MenuInflater(this); //getMenuInflater(); inflater.inflate(R.menu.menu, menu); } // 对菜单中的按钮添加响应时间 @Override public boolean onContextItemSelected(MenuItem item) { int item_id = item.getItemId(); student = (Student) listView.getTag(); Log.v(TAG, "TestSQLite++++student+" + listView.getTag() + ""); final long student_id = student.getId(); Intent intent = new Intent(); Log.v(TAG, "TestSQLite+++++++id"+student_id); switch (item_id) { /* 添加 case R.id.add: startActivity(new Intent(this, AddStudentActivity.class)); break;*/ // 删除 case R.id.delete: deleteStudentInformation(student_id); break; case R.id.look: // 查看学生信息 Log.v(TAG, "TestSQLite+++++++look"+student+""); intent.putExtra("student", student); intent.setClass(this, ShowStudentActivity.class); this.startActivity(intent); break; case R.id.write: // 修改学生信息 intent.putExtra("student", student); intent.setClass(this, AddStudentActivity.class); this.startActivity(intent); break; default: break; } return super.onContextItemSelected(item); } @Override public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) { Student student = (Student) dao.getStudentFromView(view, id); listView.setTag(student); registerForContextMenu(listView); return false; } // 点击一条记录是触发的事件 @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (!isDeleteList) { student = dao.getStudentFromView(view, id); Log.e(TAG, "student*****" + dao.getStudentFromView(view, id)); Intent intent = new Intent(); intent.putExtra("student", student); intent.setClass(this, ShowStudentActivity.class); this.startActivity(intent); } else { CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); box.setChecked(!box.isChecked()); list.add(id); deleteButton.setEnabled(box.isChecked()); } } // 自定义一个加载数据库中的全部记录到当前页面的无参方法 public void load() { StudentDBHelper studentDBHelper = new StudentDBHelper( StudentListActivity.this); SQLiteDatabase database = studentDBHelper.getWritableDatabase(); cursor = database.query(TableContanst.STUDENT_TABLE, null, null, null, null, null, TableContanst.StudentColumns.MODIFY_TIME + " desc"); startManagingCursor(cursor); adapter = new SimpleCursorAdapter(this, R.layout.student_list_item, cursor, new String[] { TableContanst.StudentColumns.ID, TableContanst.StudentColumns.NAME, TableContanst.StudentColumns.AGE, TableContanst.StudentColumns.SEX, TableContanst.StudentColumns.LIKES, TableContanst.StudentColumns.PHONE_NUMBER, TableContanst.StudentColumns.TRAIN_DATE }, new int[] { R.id.tv_stu_id, R.id.tv_stu_name, R.id.tv_stu_age, R.id.tv_stu_sex, R.id.tv_stu_likes, R.id.tv_stu_phone, R.id.tv_stu_traindate }); listView.setAdapter(adapter); } // 全选或者取消全选 private void checkOrClearAllCheckboxs(boolean b) { int childCount = listView.getChildCount(); Log.e(TAG, "list child size=" + childCount); for (int i = 0; i < childCount; i++) { View view = listView.getChildAt(i); if (view != null) { CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); box.setChecked(!b); } } showOrHiddenCheckBoxs(true); } // 显示或者隐藏自定义菜单 private void showOrHiddenCheckBoxs(boolean b) { int childCount = listView.getChildCount(); Log.e(TAG, "list child size=" + childCount); for (int i = 0; i < childCount; i++) { View view = listView.getChildAt(i); if (view != null) { CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); int visible = b ? View.VISIBLE : View.GONE; box.setVisibility(visible); layout.setVisibility(visible); deleteButton.setEnabled(false); } } } // 自定义一个利用对话框形式进行数据的删除 private void deleteStudentInformation(final long delete_id) { // 利用对话框的形式删除数据 AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("学员信息删除") .setMessage("确定删除所选记录?") .setCancelable(false) .setPositiveButton("确定", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { int raws = dao.deleteStudentById(delete_id); layout.setVisibility(View.GONE); isDeleteList = !isDeleteList; load(); if (raws > 0) { Toast.makeText(StudentListActivity.this, "删除成功!", Toast.LENGTH_LONG).show(); } else Toast.makeText(StudentListActivity.this, "删除失败!", Toast.LENGTH_LONG).show(); } }) .setNegativeButton("取消", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); } // 点击全选事件时所触发的响应 private void selectAllMethods() { // 全选,如果当前全选按钮显示是全选,则在点击后变为取消全选,如果当前为取消全选,则在点击后变为全选 if (selectAllButton.getText().toString().equals("全选")) { int childCount = listView.getChildCount(); for (int i = 0; i < childCount; i++) { View view = listView.getChildAt(i); if (view != null) { CheckBox box = (CheckBox) view.findViewById(R.id.cb_box); box.setChecked(true); deleteButton.setEnabled(true); selectAllButton.setText("取消全选"); } } } else if (selectAllButton.getText().toString().equals("取消全选")) { checkOrClearAllCheckboxs(true); deleteButton.setEnabled(false); selectAllButton.setText("全选"); } } }
布局文件:main.xml和student_list_item.xml
代码如下
main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:id="@+id/RelativeLayout" android:layout_width="fill_parent" android:layout_height="wrap_content"> <Button android:id="@+id/bn_search_id" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="搜索" android:gravity="center_vertical" /> <Button android:gravity="center" android:text="添加学员信息" android:id="@+id/btn_add_student" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_toRightOf="@+id/bn_search_id" android:layout_toLeftOf="@+id/bn_select" /> <Button android:gravity="center_vertical" android:text="选择" android:id="@+id/bn_select" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentRight="true"></Button> </RelativeLayout> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:text=" ID 姓 名 年 龄 性 别 " /> <ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" /> <LinearLayout android:orientation="horizontal" android:id="@+id/showLiner" android:visibility="gone" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center_horizontal"> <Button android:id="@+id/bn_delete" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="删除" android:enabled="false" /> <Button android:id="@+id/bn_selectall" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="全选" /> <Button android:id="@+id/bn_canel" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:text="取消" /> </LinearLayout> </LinearLayout>
student_list_item.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" > <ImageView android:layout_width="fill_parent" android:layout_height="130px" android:layout_gravity="center" android:layout_weight="1" android:background="@drawable/icon" /> <TextView android:id="@+id/tv_stu_id" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_stu_name" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_stu_age" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_stu_sex" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1"/> <TextView android:id="@+id/tv_stu_likes" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone"/> <TextView android:id="@+id/tv_stu_phone" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone"/> <TextView android:id="@+id/tv_stu_traindate" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="1" android:visibility="gone"/> <TextView android:id="@+id/tv_stu_modifyDateTime" android:layout_width="fill_parent" android:layout_gravity="center" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone"/> <CheckBox android:id="@+id/cb_box" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_weight="1" android:visibility="gone" android:checked="false" android:focusable="false"/> </LinearLayout>
展示单条记录详细信息的ShowStudentActivity
代码如下
java文件:ShowStudentActivity
package com.example.asus.student; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.TextView; import Student.Student; import TableContanst.TableContanst; public class ShowStudentActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.student_info); Intent intent = getIntent(); Student student = (Student) intent.getSerializableExtra(TableContanst.STUDENT_TABLE); ((TextView)findViewById(R.id.tv_info_id)).setText(student.getId()+""); ((TextView)findViewById(R.id.tv_info_name)).setText(student.getName()); ((TextView)findViewById(R.id.tv_info_age)).setText(student.getAge()+""); ((TextView)findViewById(R.id.tv_info_sex)).setText(student.getSex()); ((TextView)findViewById(R.id.tv_info_likes)).setText(student.getLike()); ((TextView)findViewById(R.id.tv_info_train_date)).setText(student.getTrainDate()); ((TextView)findViewById(R.id.tv_info_phone)).setText(student.getPhoneNumber()); } public void goBack(View view) { finish(); } }
布局文件:student_info.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="5dip" > <TextView android:id="@+id/id2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:textSize="16sp" android:gravity="left|center_vertical" android:text="学员ID:" /> <TextView android:id="@+id/tv_info_id" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/id2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/id2_text_id" android:gravity="left|center_vertical"/> <TextView android:id="@+id/name2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:layout_marginRight="5dip" android:layout_marginTop="5dip" android:layout_marginBottom="5dip" android:layout_below="@id/id2_text_id" android:layout_alignLeft="@id/id2_text_id" android:textSize="16sp" android:gravity="left|center_vertical" android:text="姓名:" /> <TextView android:id="@+id/tv_info_name" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/name2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/name2_text_id" android:gravity="left|center_vertical" /> <TextView android:id="@+id/age2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="left|center_vertical" android:layout_marginRight="5dip" android:layout_below="@id/name2_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="年龄:" /> <TextView android:id="@+id/tv_info_age" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/age2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/age2_text_id" android:gravity="left|center_vertical" /> <TextView android:id="@+id/sex2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="left|center_vertical" android:layout_below="@id/age2_text_id" android:layout_alignLeft="@id/age2_text_id" android:layout_marginRight="5dip" android:layout_marginBottom="5dip" android:text="性别:" android:textSize="16sp" /> <TextView android:id="@+id/tv_info_sex" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/sex2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/sex2_text_id" android:gravity="left|center_vertical" /> <TextView android:id="@+id/like2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="left|center_vertical" android:layout_below="@id/sex2_text_id" android:layout_alignLeft="@id/sex2_text_id" android:layout_marginRight="5dip" android:layout_marginBottom="5dip" android:text="爱好:" android:textSize="16sp" /> <TextView android:layout_height="40dip" android:id="@+id/tv_info_likes" android:layout_width="wrap_content" android:layout_toRightOf="@id/like2_text_id" android:layout_below="@id/sex2_text_id" android:layout_marginRight="52dip" android:gravity="left|center_vertical"/> <TextView android:id="@+id/contact2_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|left" android:layout_marginRight="5dip" android:layout_below="@id/like2_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="联系电话:" /> <TextView android:id="@+id/tv_info_phone" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/contact2_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/contact2_text_id" android:gravity="center_vertical|left" /> <TextView android:id="@+id/train2_time_text_id" android:layout_width="80dip" android:layout_height="40dip" android:gravity="center_vertical|left" android:layout_marginRight="5dip" android:layout_below="@id/contact2_text_id" android:layout_marginBottom="5dip" android:textSize="16sp" android:text="入学日期" /> <TextView android:id="@+id/tv_info_train_date" android:layout_width="fill_parent" android:layout_height="40dip" android:layout_toRightOf="@id/train2_time_text_id" android:layout_alignParentRight="true" android:layout_alignTop="@id/train2_time_text_id" android:gravity="center_vertical|left" /> <Button android:id="@+id/back_to_list_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="返回列表界面" android:layout_below="@id/train2_time_text_id" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:onClick="goBack"> </Button> </RelativeLayout>
添加记录的活动AddStudentActivity
代码如下
相关阅读 >>
开源 5 款超好用的数据库 gui 带你玩转 mongodb、redis、sql 数据库(推荐)
android Sqlite事务处理结合listview列表显示功能示例
微软官方sqlhelper类 数据库辅助操作类 font color=red原创font
更多相关阅读请进入《Sqlite》频道 >>
数据库系统概念 第6版
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。