2020-06-09

基于安卓的学生管理系统

转载https://blog.csdn.net/sinat_34608734/article/details/74136271?utm_medium=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase&depth_1-utm_source=distribute.wap_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-1.nonecase

利用ListView和数据库做一个学员信息管理系统。下面把做的代码复制下来,供大家参考。

首页的布局main.xml

复制代码













复制代码 创建listView中显示学员信息的xml格式 student_list_item.xml

?






  




      

  创建一个StudentListActivity做为主页显示学员信息以及进行一些操作。

复制代码
package cn.yj3g.student.activity;

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 cn.yj3g.student.dao.StudentDao;
import cn.yj3g.student.db.StudentDBHelper;
import cn.yj3g.student.entry.Student;
import cn.yj3g.student.entry.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 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();
    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);

}

@Override
protected void onStart() {
    // 调用load()方法将数据库中的所有记录显示在当前页面
    super.onStart();
    load();

}

public void onClick(View v) {
    // 跳转到添加信息的界面
    if (v == addStudent) {
        startActivity(new Intent(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();
    }
}
// 创建菜单
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.layout.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 onCreateOptionsMenu(Menu menu) {
    menu.add(1, 1, 1, "按入学日期排序");
    menu.add(1, 2, 1, "按姓名进行排序");
    menu.add(1, 5, 1, "按学号进行排序");
    menu.add(1, 3, 1, "模糊查找");
    menu.add(1, 4, 1, "退出");
    return super.onCreateOptionsMenu(menu);
}

// 对菜单中的按钮添加响应时间
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
    // 排序
    case 1:
        cursor = dao.sortByTrainDate();
        load(cursor);
        break;

    // 排序
    case 2:
        cursor = dao.sortByName();
        load(cursor);
        break;
    // 查找
    case 3:
        startActivity(new Intent(this, StudentSearch.class));
        break;
    // 退出
    case 4:
        finish();
        break;
    case 5:
        cursor = dao.sortByID();
        load(cursor);
        break;
    default:
        break;
    }
    return super.onOptionsItemSelected(item);
}

// 长点击一条记录触发的时间
@Override
public boolean onItemLongClick(AdapterView parent, View view,
        int position, long id) {
    Student student = (Student) dao.getStudentFromView(view, id);
    listView.setTag(student);
    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);
}

// 自定义一个加载数据库中的全部记录到当前页面的有参方法
public void load(Cursor 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("全选");
    }
}

}
复制代码
menu.xml文件

复制代码
1


2
3
4
5
6
7
8
9
复制代码
界面效果图如下:

删除界面:

在点击ListView中的一条Item时,老师要求显示出这个学生的详细信息,所以需要一个xml文件来显示这个信息。在这里我创建了一个student_info.xml文件

复制代码
1
2 3 android:orientation=“vertical”
4 android:layout_width=“fill_parent”
5 android:layout_height=“fill_parent”
6 android:padding=“5dip”
7 >
8 9 android:layout_width=“80dip”
10 android:layout_height=“40dip”
11 android:layout_marginRight=“5dip”
12 android:layout_marginTop=“5dip”
13 android:layout_marginBottom=“5dip”
14 android:textSize=“16sp”
15 android:gravity=“left|center_vertical”
16 android:text=“学员ID:”
17 />
18
19 20 android:layout_width=“fill_parent”
21 android:layout_height=“40dip”
22 android:layout_toRightOf="@id/id2_text_id"
23 android:layout_alignParentRight=“true”
24 android:layout_alignTop="@id/id2_text_id"
25 android:gravity=“left|center_vertical”
26 />
27
28 29 android:layout_width=“80dip”
30 android:layout_height=“40dip”
31 android:layout_marginRight=“5dip”
32 android:layout_marginTop=“5dip”
33 android:layout_marginBottom=“5dip”
34 android:layout_below="@id/id2_text_id"
35 android:layout_alignLeft="@id/id2_text_id"
36 android:textSize=“16sp”
37 android:gravity=“left|center_vertical”
38 android:text=“姓名:”
39 />
40
41 42 android:layout_width=“fill_parent”
43 android:layout_height=“40dip”
44 android:layout_toRightOf="@id/name2_text_id"
45 android:layout_alignParentRight=“true”
46 android:layout_alignTop="@id/name2_text_id"
47 android:gravity=“left|center_vertical”
48 />
49
50 51 android:layout_width=“80dip”
52 android:layout_height=“40dip”
53 android:gravity=“left|center_vertical”
54 android:layout_marginRight=“5dip”
55 android:layout_below="@id/name2_text_id"
56 android:layout_marginBottom=“5dip”
57 android:textSize=“16sp”
58 android:text=“年龄:”
59 />
60
61 62 android:layout_width=“fill_parent”
63 android:layout_height=“40dip”
64 android:layout_toRightOf="@id/age2_text_id"
65 android:layout_alignParentRight=“true”
66 android:layout_alignTop="@id/age2_text_id"
67 android:gravity=“left|center_vertical”
68 />
69
70 71 android:layout_width=“80dip”
72 android:layout_height=“40dip”
73 android:gravity=“left|center_vertical”
74 android:layout_below="@id/age2_text_id"
75 android:layout_alignLeft="@id/age2_text_id"
76 android:layout_marginRight=“5dip”
77 android:layout_marginBottom=“5dip”
78 android:text=“性别:”
79 android:textSize=“16sp”
80 />
81
82 83 android:id="@+id/tv_info_sex"
84 android:layout_width=“fill_parent”
85 android:layout_height=“40dip”
86 android:layout_toRightOf="@id/sex2_text_id"
87 android:layout_alignParentRight=“true”
88 android:layout_alignTop="@id/sex2_text_id"
89 android:gravity=“left|center_vertical”
90 />
91
92 93 android:layout_width=“80dip”
94 android:layout_height=“40dip”
95 android:gravity=“left|center_vertical”
96 android:layout_below="@id/sex2_text_id"
97 android:layout_alignLeft="@id/sex2_text_id"
98 android:layout_marginRight=“5dip”
99 android:layout_marginBottom=“5dip”
100 android:text=“爱好:”
101 android:textSize=“16sp”
102 />
103 104 android:id="@+id/tv_info_likes"
105 android:layout_width=“wrap_content”
106 android:layout_toRightOf="@id/like2_text_id"
107 android:layout_below="@id/sex2_text_id"
108 android:layout_marginRight=“52dip”
109 android:gravity=“left|center_vertical”/>
110
111 112 android:layout_width=“80dip”
113 android:layout_height=“40dip”
114 android:gravity=“center_vertical|left”
115 android:layout_marginRight=“5dip”
116 android:layout_below="@id/like2_text_id"
117 android:layout_marginBottom=“5dip”
118 android:textSize=“16sp”
119 android:text=“联系电话:”
120 />
121
122 123 android:layout_width=“fill_parent”
124 android:layout_height=“40dip”
125 android:layout_toRightOf="@id/contact2_text_id"
126 android:layout_alignParentRight=“true”
127 android:layout_alignTop="@id/contact2_text_id"
128 android:gravity=“center_vertical|left”
129 />
130
131 132 android:layout_width=“80dip”
133 android:layout_height=“40dip”
134 android:gravity=“center_vertical|left”
135 android:layout_marginRight=“5dip”
136 android:layout_below="@id/contact2_text_id"
137 android:layout_marginBottom=“5dip”
138 android:textSize=“16sp”
139 android:text=“入学日期”
140 />
141
142 143 android:layout_width=“fill_parent”
144 android:layout_height=“40dip”
145 android:layout_toRightOf="@id/train2_time_text_id"
146 android:layout_alignParentRight=“true”
147 android:layout_alignTop="@id/train2_time_text_id"
148 android:gravity=“center_vertical|left”
149 />
150
151

你可能感兴趣的:(笔记)