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

首页的布局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="@string/myButton"
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_weight="1"
android:layout_height="wrap_content"/>
<LinearLayout 
android:orientation="horizontal"
android:id="@+id/showLiner"
android:visibility="gone"    
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<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>
复制代码

创建listView中显示学员信息的xml格式 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="wrap_content"
        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>

  创建一个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<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);
}
@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 <menu xmlns:android="http://schemas.android.com/apk/res/android">
2 
3     <group android:checkableBehavior="single">
4         <item android:id="@+id/delete" android:title="删除学员信息" />
5         <item android:id="@+id/look" android:title="详细信息" />
6         <item android:id="@+id/add" android:title="添加学员信息" />
7         <item android:id="@+id/write" android:title="修改学员信息" />
8     </group>
9 </menu>
复制代码

界面效果图如下:

删除界面:

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

复制代码
  1 <?xml version="1.0" encoding="utf-8"?>
  2 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:orientation="vertical"
  4     android:layout_width="fill_parent"
  5     android:layout_height="fill_parent"
  6     android:padding="5dip"
  7     >
  8     <TextView android:id="@+id/id2_text_id"
  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     <TextView android:id="@+id/tv_info_id"
 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     <TextView android:id="@+id/name2_text_id"
 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     <TextView android:id="@+id/tv_info_name"
 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     <TextView android:id="@+id/age2_text_id"
 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     <TextView android:id="@+id/tv_info_age"
 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     <TextView android:id="@+id/sex2_text_id"
 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     <TextView
 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     <TextView  android:id="@+id/like2_text_id"
 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     <TextView android:layout_height="40dip" 
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     <TextView android:id="@+id/contact2_text_id"
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     <TextView android:id="@+id/tv_info_phone"
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     <TextView android:id="@+id/train2_time_text_id"
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     <TextView android:id="@+id/tv_info_train_date"
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     <Button android:id="@+id/back_to_list_id" 
152         android:layout_width="fill_parent" 
153         android:layout_height="wrap_content" 
154         android:text="返回列表界面" 
155         android:layout_below="@id/train2_time_text_id" 
156         android:layout_alignParentLeft="true"
157         android:layout_alignParentRight="true"
158         android:onClick="goBack">
159     </Button>
160 </RelativeLayout>
复制代码

当然我们需要一个Activity来显示这些信息,在这里我创建了一个ShowStudentActivity。

复制代码
 1 package cn.yj3g.student.activity;
 2 
 3 import android.app.Activity;
 4 import android.content.Intent;
 5 import android.os.Bundle;
 6 import android.view.View;
 7 import android.widget.TextView;
 8 import cn.yj3g.student.entry.Student;
 9 import cn.yj3g.student.entry.TableContanst;
10 
11 public class ShowStudentActivity extends Activity {
12 
13     @Override
14     public void onCreate(Bundle savedInstanceState) {
15         super.onCreate(savedInstanceState);
16         setContentView(R.layout.student_info);
17         Intent intent = getIntent();
18         Student student = (Student) intent.getSerializableExtra(TableContanst.STUDENT_TABLE);
19         ((TextView)findViewById(R.id.tv_info_id)).setText(student.getId()+"");
20         ((TextView)findViewById(R.id.tv_info_name)).setText(student.getName());
21         ((TextView)findViewById(R.id.tv_info_age)).setText(student.getAge()+"");
22         ((TextView)findViewById(R.id.tv_info_sex)).setText(student.getSex());
23         ((TextView)findViewById(R.id.tv_info_likes)).setText(student.getLike());
24         ((TextView)findViewById(R.id.tv_info_train_date)).setText(student.getTrainDate());
25         ((TextView)findViewById(R.id.tv_info_phone)).setText(student.getPhoneNumber());
26     }
27     
28     public void goBack(View view) {
29         finish();
30     }
31 }
复制代码

界面效果如下:

最最重要的环节来了,我们做这个系统主要是为了管理学员,所以增加和修改学员信息是必不可少的,因此需要一个Activity来专门提供添加和修改学员信息。在这里还有一件重要的事情要做,那就是要设计添加和修改学员的界面,在这里可用用相对布局做比较简单。我在这里创建了一个AddStudentActivity来添加和修改学员信息,下面附上相关代码:

add_student.xml

复制代码
  1 <?xml version="1.0" encoding="utf-8"?>
  2 
  3 <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"  
  4     android:layout_width="fill_parent"  
  5     android:layout_height="fill_parent"  
  6     android:fillViewport="true"  
  7     android:scrollbarStyle="outsideInset" > 
  8 <RelativeLayout 
  9     android:orientation="vertical"
 10     android:layout_width="fill_parent"
 11     android:layout_height="fill_parent"
 12     android:padding="5dip"
 13     >
 14     
 15     <TextView android:id="@+id/tv_stu_text_id"
 16         android:layout_width="80dip"
 17         android:layout_height="40dip"
 18         android:gravity="center_vertical|right"
 19         android:layout_marginRight="5dip"
 20         android:layout_marginTop="5dip"
 21         android:layout_marginBottom="5dip"
 22         android:textSize="16sp"
 23         android:text="学员ID:"
 24     />
 25     
 26     <TextView android:id="@+id/tv_stu_id"
 27         android:layout_width="fill_parent"
 28         android:layout_height="40dip"
 29         android:text="未分配ID"
 30         android:layout_toRightOf="@id/tv_stu_text_id"
 31         android:layout_alignParentRight="true"
 32         android:layout_alignTop="@id/tv_stu_text_id"
 33         android:gravity="center"
 34         android:background="#ffffff"
 35         android:textColor="#000000"
 36         android:textSize="16sp"
 37     />
 38     
 39     <TextView android:id="@+id/tv_name_text"
 40         android:layout_width="80dip"
 41         android:layout_height="40dip"
 42         android:gravity="center_vertical|right"
 43         android:layout_marginRight="5dip"
 44         android:layout_below="@id/tv_stu_text_id"
 45         android:layout_alignLeft="@id/tv_stu_text_id"
 46         android:layout_marginBottom="5dip"
 47         android:textSize="16sp"
 48         android:text="姓名:"
 49     />
 50     
 51     <EditText android:id="@+id/et_name"
 52         android:layout_width="fill_parent"
 53         android:layout_height="40dip"
 54         android:layout_toRightOf="@id/tv_name_text"
 55         android:layout_alignParentRight="true"
 56         android:layout_alignTop="@id/tv_name_text"
 57         android:hint="请输入姓名"
 58         android:inputType="textPersonName"
 59         android:paddingLeft="20dip"/>
 60     
 61     <TextView android:id="@+id/tv_age_text"
 62         android:layout_width="80dip"
 63         android:layout_height="40dip"
 64         android:gravity="center_vertical|right"
 65         android:layout_marginRight="5dip"
 66         android:layout_below="@id/tv_name_text"
 67         android:layout_marginBottom="5dip"
 68         android:textSize="16sp"
 69         android:text="年龄:"
 70     />
 71     
 72     <EditText android:id="@+id/et_age"
 73         android:layout_width="fill_parent"
 74         android:layout_height="40dip"
 75         android:layout_toRightOf="@id/tv_age_text"
 76         android:layout_alignParentRight="true"
 77         android:layout_alignTop="@id/tv_age_text"
 78         android:hint="请输入年龄"
 79         android:paddingLeft="20dip"
 80         android:maxLength="3"
 81         android:inputType="number"
 82     />
 83     
 84     <TextView android:id="@+id/tv_sex_text"
 85         android:layout_width="80dip"
 86         android:layout_height="40dip"
 87         android:gravity="center_vertical|right"
 88         android:layout_below="@id/tv_age_text"
 89         android:layout_alignLeft="@id/tv_age_text"
 90         android:layout_marginRight="5dip"
 91         android:layout_marginBottom="5dip"
 92         android:text="性别:"
 93         android:textSize="16sp"
 94     />
 95     
 96     <RadioGroup
 97         android:id="@+id/rg_sex"
 98         android:layout_width="fill_parent"
 99         android:layout_height="40dip"
100         android:orientation="horizontal"
101         android:layout_toRightOf="@id/tv_sex_text"
102         android:layout_alignParentRight="true"
103         android:layout_alignTop="@id/tv_sex_text"
104         >
105         <RadioButton
106           android:id="@+id/rb_sex_male"
107           android:layout_width="wrap_content"
108           android:layout_height="wrap_content"
109           android:layout_weight="1"
110           android:text=""
111           android:textSize="16sp"
112         />
113         <RadioButton android:layout_width="wrap_content" 
114         android:layout_height="wrap_content" 
115         android:text="" 
116         android:id="@+id/rb_sex_female"
117         android:layout_weight="1"
118         android:textSize="16sp">
119         </RadioButton>
120     </RadioGroup>
121     
122     <TextView  android:id="@+id/tv_likes_text"
123         android:layout_width="80dip"
124         android:layout_height="40dip"
125         android:gravity="center_vertical|right"
126         android:layout_below="@id/rg_sex"
127         android:layout_alignLeft="@id/tv_sex_text"
128         android:layout_marginRight="5dip"
129         android:layout_marginBottom="5dip"
130         android:text="爱好:"
131         android:textSize="16sp"
132     />
133     <CheckBox
134     android:id="@+id/box1"
135             android:layout_width="wrap_content" android:layout_height="wrap_content"
136             android:layout_toRightOf="@id/tv_likes_text"
137             android:layout_below="@+id/rg_sex"
138             android:layout_alignLeft="@+id/group1"
139             android:text="@string/box1"
140     ></CheckBox>
141     <CheckBox
142     android:id="@+id/box2"
143             android:layout_width="wrap_content" android:layout_height="wrap_content"
144             android:layout_toRightOf="@+id/box1"
145             android:layout_below="@+id/rg_sex"
146             android:layout_alignTop="@+id/box1"
147             android:text="@string/box2"
148     ></CheckBox>
149     <CheckBox
150     android:id="@+id/box3"
151             android:layout_width="wrap_content" android:layout_height="wrap_content"
152             android:layout_toRightOf="@+id/box2"
153             android:layout_below="@+id/rg_sex"
154             android:layout_alignTop="@+id/box2"
155             android:text="@string/box3"
156     ></CheckBox>
157     
158     <TextView android:id="@+id/tv_phone_text"
159         android:layout_width="80dip"
160         android:layout_height="40dip"
161         android:gravity="center_vertical|right"
162         android:layout_marginRight="5dip"
163         android:layout_below="@id/tv_likes_text"
164         android:layout_marginBottom="5dip"
165         android:textSize="16sp"
166         android:text="联系电话:"
167     />
168     
169     <EditText android:id="@+id/et_phone"
170         android:layout_width="fill_parent"
171         android:layout_height="40dip"
172         android:layout_toRightOf="@id/tv_phone_text"
173         android:layout_alignParentRight="true"
174         android:layout_alignTop="@id/tv_phone_text"
175         android:hint="请输入手机号"
176         android:paddingLeft="20dip"
177         android:inputType="phone"
178         android:maxLength="11"
179     />
180     
181     <TextView android:id="@+id/tv_traindate_text"
182         android:layout_width="80dip"
183         android:layout_height="40dip"
184         android:gravity="center_vertical|right"
185         android:layout_marginRight="5dip"
186         android:layout_below="@id/tv_phone_text"
187         android:layout_marginBottom="5dip"
188         android:textSize="16sp"
189         android:text="入学日期"
190         
191     />
192     
193     <EditText android:id="@+id/et_traindate"
194         android:layout_width="fill_parent"
195         android:layout_height="40dip"
196         android:layout_toRightOf="@id/tv_traindate_text"
197         android:layout_alignParentRight="true"
198         android:layout_alignTop="@id/tv_traindate_text"
199         android:hint="点击选择日期"
200         android:inputType="date"
201         android:paddingLeft="20dip"
202         android:focusable="false"
203     />
204     <Button android:id="@+id/btn_save" 
205         android:layout_width="wrap_content" 
206         android:layout_height="wrap_content" 
207         android:text="保存" 
208         android:layout_below="@id/tv_traindate_text" 
209         android:layout_alignRight="@id/rg_sex">
210     </Button>
211     <Button android:id="@+id/btn_clear" 
212         android:layout_width="wrap_content" 
213         android:layout_height="wrap_content" 
214         android:text="重置" 
215         android:layout_below="@id/tv_traindate_text" 
216         android:layout_toLeftOf="@id/btn_save"
217         android:layout_marginRight="10dip">
218     </Button>
219 </RelativeLayout>
220 </ScrollView>
复制代码

AddStudentActivity代码:

复制代码
  1 package cn.yj3g.student.activity;
  2 
  3 import java.io.Serializable;
  4 import java.text.SimpleDateFormat;
  5 import java.util.Calendar;
  6 import java.util.Date;
  7 import java.util.HashSet;
  8 
  9 import android.app.Activity;
 10 import android.app.DatePickerDialog;
 11 import android.app.Dialog;
 12 import android.content.ContentValues;
 13 import android.content.Intent;
 14 import android.database.sqlite.SQLiteDatabase;
 15 import android.os.Bundle;
 16 import android.view.View;
 17 import android.view.View.OnClickListener;
 18 import android.widget.Button;
 19 import android.widget.CheckBox;
 20 import android.widget.CompoundButton;
 21 import android.widget.CompoundButton.OnCheckedChangeListener;
 22 import android.widget.DatePicker;
 23 import android.widget.EditText;
 24 import android.widget.RadioButton;
 25 import android.widget.RadioGroup;
 26 import android.widget.TextView;
 27 import android.widget.Toast;
 28 import cn.yj3g.student.dao.StudentDao;
 29 import cn.yj3g.student.db.StudentDBHelper;
 30 import cn.yj3g.student.entry.Student;
 31 import cn.yj3g.student.entry.TableContanst;
 32 
 33 public class AddStudentActivity extends Activity implements OnClickListener {
 34 
 35     private static final String TAG = "AddStudentActivity";
 36     private final static int DATE_DIALOG = 1;
 37     private static final int DATE_PICKER_ID = 1;
 38     private TextView idText;
 39     private EditText nameText;
 40     private EditText ageText;
 41     private EditText phoneText;
 42     private EditText dataText;
 43     private RadioGroup group;
 44     private RadioButton button1;
 45     private RadioButton button2;
 46     private CheckBox box1;
 47     private CheckBox box2;
 48     private CheckBox box3;
 49     private Button restoreButton;
 50     private String sex;
 51     private Button resetButton;
 52     private Long student_id;
 53 
 54     private StudentDao dao;
 55     private boolean isAdd = true;
 56 
 57     @Override
 58     public void onCreate(Bundle savedInstanceState) {
 59         super.onCreate(savedInstanceState);
 60         setContentView(R.layout.add_student);
 61         idText = (TextView) findViewById(R.id.tv_stu_id);
 62         nameText = (EditText) findViewById(R.id.et_name);
 63         ageText = (EditText) findViewById(R.id.et_age);
 64         button1 = (RadioButton) findViewById(R.id.rb_sex_female);
 65         button2 = (RadioButton) findViewById(R.id.rb_sex_male);
 66         phoneText = (EditText) findViewById(R.id.et_phone);
 67         dataText = (EditText) findViewById(R.id.et_traindate);
 68         group = (RadioGroup) findViewById(R.id.rg_sex);
 69         box1 = (CheckBox) findViewById(R.id.box1);
 70         box2 = (CheckBox) findViewById(R.id.box2);
 71         box3 = (CheckBox) findViewById(R.id.box3);
 72 
 73         restoreButton = (Button) findViewById(R.id.btn_save);
 74         resetButton = (Button) findViewById(R.id.btn_clear);
 75         dao = new StudentDao(new StudentDBHelper(this));
 76 
 77         // 设置监听
 78         restoreButton.setOnClickListener(this);
 79         resetButton.setOnClickListener(this);
 80         dataText.setOnClickListener(this);
 81         checkIsAddStudent();
 82     }
 83 
 84     /**
 85      * 检查此时Activity是否用于添加学员信息
 86      */
 87     private void checkIsAddStudent() {
 88         Intent intent = getIntent();
 89         Serializable serial = intent
 90                 .getSerializableExtra(TableContanst.STUDENT_TABLE);
 91         if (serial == null) {
 92             isAdd = true;
 93             dataText.setText(getCurrentDate());
 94         } else {
 95             isAdd = false;
 96             Student s = (Student) serial;
 97             showEditUI(s);
 98 
 99         }
100     }
101 
102     /**
103      * 显示学员信息更新的UI
104      */
105     private void showEditUI(Student student) {
106         // 先将Student携带的数据还原到student的每一个属性中去
107         student_id = student.getId();
108         String name = student.getName();
109         int age = student.getAge();
110         String phone = student.getPhoneNumber();
111         String data = student.getTrainDate();
112         String like = student.getLike();
113         String sex = student.getSex();
114         if (sex.toString().equals("")) {
115             button2.setChecked(true);
116         } else if (sex.toString().equals("")) {
117             button1.setChecked(true);
118         }
119         if (like != null && !"".equals(like)) {
120             if (box1.getText().toString().indexOf(like) >= 0) {
121                 box1.setChecked(true);
122             }
123             if (box2.getText().toString().indexOf(like) >= 0) {
124                 box2.setChecked(true);
125             }
126             if (box3.getText().toString().indexOf(like) >= 0) {
127                 box3.setChecked(true);
128             }
129         }
130         // 还原数据
131         idText.setText(student_id + "");
132         nameText.setText(name + "");
133         ageText.setText(age + "");
134         phoneText.setText(phone + "");
135         dataText.setText(data + "");
136         setTitle("学员信息更新");
137         restoreButton.setText("更新");
138     }
139 
140     public void onClick(View v) {
141         // 收集数据
142         if (v == restoreButton) {
143             if (!checkUIInput()) {// 界面输入验证
144                 return;
145             }
146             Student student = getStudentFromUI();
147             if (isAdd) {
148                 long id = dao.addStudent(student);
149                 dao.closeDB();
150                 if (id > 0) {
151                     Toast.makeText(this, "保存成功, ID=" + id, 0).show();
152                     finish();
153                 } else {
154                     Toast.makeText(this, "保存失败,请重新输入!", 0).show();
155                 }
156             } else if (!isAdd) {
157                 long id = dao.addStudent(student);
158                 dao.closeDB();
159                 if (id > 0) {
160                     Toast.makeText(this, "更新成功", 0).show();
161                     finish();
162                 } else {
163                     Toast.makeText(this, "更新失败,请重新输入!", 0).show();
164                 }
165             }
166 
167         } else if (v == resetButton) {
168             clearUIData();
169         } else if (v == dataText) {
170             showDialog(DATE_PICKER_ID);
171         }
172     }
173 
174     /**
175      * 清空界面的数据
176      */
177     private void clearUIData() {
178         nameText.setText("");
179         ageText.setText("");
180         phoneText.setText("");
181         dataText.setText("");
182         box1.setChecked(false);
183         box2.setChecked(false);
184         group.clearCheck();
185     }
186 
187     /**
188      * 收集界面输入的数据,并将封装成Student对象
189      */
190     private Student getStudentFromUI() {
191         String name = nameText.getText().toString();
192         int age = Integer.parseInt(ageText.getText().toString());
193         String sex = ((RadioButton) findViewById(group
194                 .getCheckedRadioButtonId())).getText().toString();
195         String likes = "";
196         if (box1.isChecked()) { // basketball, football football
197             likes += box1.getText();
198         }
199         if (box2.isChecked()) {
200             if (likes.equals("")) {
201                 likes += box2.getText();
202             } else {
203                 likes += "," + box2.getText();
204             }
205             if (likes.equals("")) {
206                 likes += box3.getText();
207             } else {
208                 likes += "," + box3.getText();
209             }
210         }
211         String trainDate = dataText.getText().toString();
212         String phoneNumber = phoneText.getText().toString();
213         String modifyDateTime = getCurrentDateTime();
214         Student s=new Student(name, age, sex, likes, phoneNumber, trainDate,
215                 modifyDateTime);
216         if (!isAdd) {
217             s.setId(Integer.parseInt(idText.getText().toString()));
218             dao.deleteStudentById(student_id);
219         }
220         return s;
221     }
222 
223     /**
224      * 得到当前的日期时间
225      */
226     private String getCurrentDateTime() {
227         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
228         return format.format(new Date());
229     }
230     /**
231      * 得到当前的日期
232      */
233     private String getCurrentDate() {
234         SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
235         return format.format(new Date());
236     }
237     /**
238      * 验证用户是否按要求输入了数据
239      */
240     private boolean checkUIInput() { // name, age, sex
241         String name = nameText.getText().toString();
242         String age = ageText.getText().toString();
243         int id = group.getCheckedRadioButtonId();
244         String message = null;
245         View invadView = null;
246         if (name.trim().length() == 0) {
247             message = "请输入姓名!";
248             invadView = nameText;
249         } else if (age.trim().length() == 0) {
250             message = "请输入年龄!";
251             invadView = ageText;
252         } else if (id == -1) {
253             message = "请选择性别!";
254         }
255         if (message != null) {
256             Toast.makeText(this, message, 0).show();
257             if (invadView != null)
258                 invadView.requestFocus();
259             return false;
260         }
261         return true;
262     }
263 
264     private DatePickerDialog.OnDateSetListener onDateSetListener = new DatePickerDialog.OnDateSetListener() {
265         @Override
266         public void onDateSet(DatePicker view, int year, int monthOfYear,
267                 int dayOfMonth) {
268             dataText.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
269         }
270     };
271 
272     @Override
273     protected Dialog onCreateDialog(int id) {
274         switch (id) {
275         case DATE_PICKER_ID:
276             return new DatePickerDialog(this, onDateSetListener, 2011, 8, 14);
277         }
278         return null;
279     }
280 }
复制代码

界面效果如下:

修改界面:

在这里我还做了一个额外的功能就是模糊查找,有想法的可以参考下。

StudentSearch代码:

复制代码
package cn.yj3g.student.activity;
import cn.yj3g.student.dao.StudentDao;
import cn.yj3g.student.db.StudentDBHelper;
import cn.yj3g.student.entry.TableContanst;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class StudentSearch extends Activity implements OnClickListener {
private EditText nameText;
private Button button;
private Button reButton;
private Cursor cursor;
private SimpleCursorAdapter adapter;
private ListView listView;
private StudentDao dao;
private Button returnButton;
private LinearLayout layout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search);
nameText = (EditText) findViewById(R.id.et_srarch);
layout=(LinearLayout) findViewById(R.id.linersearch);
button = (Button) findViewById(R.id.bn_sure_search);
reButton = (Button) findViewById(R.id.bn_return);
listView = (ListView) findViewById(R.id.searchListView);
returnButton = (Button) findViewById(R.id.return_id);
dao = new StudentDao(new StudentDBHelper(this));
reButton.setOnClickListener(this);
returnButton.setOnClickListener(this);
button.setOnClickListener(this);    
}
@Override
public void onClick(View v) {
if (v == button) {
reButton.setVisibility(View.GONE);    
button.setVisibility(View.GONE);
nameText.setVisibility(View.GONE);
layout.setVisibility(View.VISIBLE);
String name = nameText.getText().toString();
cursor = dao.findStudent(name);
if (!cursor.moveToFirst()) {
Toast.makeText(this, "没有所查学员信息!", Toast.LENGTH_SHORT).show();
} else
//如果有所查询的信息,则将查询结果显示出来
                adapter = new SimpleCursorAdapter(this, R.layout.find_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);
}else if(v==reButton|v==returnButton){
finish();
}
}
}
复制代码

search.xml

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical"
 4     android:layout_width="fill_parent" 
 5     android:layout_height="fill_parent" >
 6     <EditText 
 7         android:id="@+id/et_srarch"
 8         android:layout_width="fill_parent" 
 9         android:layout_height="wrap_content"
10         android:hint="请输入学员姓名"
11         android:inputType="textPersonName"
12     />
13     <Button 
14         android:id="@+id/bn_sure_search"
15         android:gravity="center"
16         android:layout_width="fill_parent" 
17         android:layout_height="wrap_content"
18         android:text="确定"
19         />
20         <Button 
21         android:id="@+id/bn_return"
22         android:gravity="center"
23         android:layout_width="fill_parent" 
24         android:layout_height="wrap_content"
25         android:text="返回"
26         />
27         
28 <LinearLayout android:id="@+id/linersearch"
29                 android:orientation="vertical"
30                 android:visibility="gone"
31                 android:layout_width="fill_parent" 
32                 android:layout_height="wrap_content">
33     <ListView
34     android:id="@+id/searchListView"
35     android:layout_weight="1"
36     android:layout_width="fill_parent"
37     android:layout_height="wrap_content"/>
38     <Button 
39     android:id="@+id/return_id"
40     android:layout_width="fill_parent"
41     android:layout_height="wrap_content"
42     android:text="返回"
43     />                
44 </LinearLayout>
45 </LinearLayout>
复制代码

find_student_list_item.xml

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="horizontal" android:layout_width="fill_parent"
 4     android:layout_height="wrap_content">
 5     <ImageView android:layout_width="fill_parent"
 6         android:layout_height="wrap_content" android:layout_weight="1"
 7         android:background="@drawable/icon" />
 8         <TextView android:id="@+id/tv_stu_id" android:layout_width="fill_parent"
 9         android:layout_height="wrap_content" android:layout_weight="1" />
10     <TextView android:id="@+id/tv_stu_name" android:layout_width="fill_parent"
11         android:layout_height="wrap_content" android:layout_weight="1" />
12     <TextView android:id="@+id/tv_stu_age" android:layout_width="fill_parent"
13         android:layout_height="wrap_content" android:layout_weight="1" />
14     <TextView android:id="@+id/tv_stu_sex" android:layout_width="fill_parent"
15         android:layout_height="wrap_content" android:layout_weight="1" />
16     <TextView android:id="@+id/tv_stu_likes" android:layout_width="fill_parent"
17         android:layout_height="wrap_content" android:layout_weight="1" />
18     <TextView android:id="@+id/tv_stu_phone"
19         android:layout_width="fill_parent" android:layout_height="wrap_content"
20         android:layout_weight="1" />
21     <TextView android:id="@+id/tv_stu_traindate" android:layout_width="fill_parent"
22         android:layout_height="wrap_content" android:layout_weight="1" />
23 
24 </LinearLayout>
复制代码

界面效果如下:

查询结果界面:

在这里还牵扯到了一些常量和数据库的连接以及增删改查等操作,我用我们老师的方法将他们分别放在不同的包里。下面附上代码:

StudentDao类

复制代码
  1 package cn.yj3g.student.dao;
  2 
  3 import java.util.ArrayList;
  4 import java.util.HashMap;
  5 import java.util.List;
  6 import java.util.Map;
  7 import android.content.ContentValues;
  8 import android.database.Cursor;
  9 import android.database.sqlite.SQLiteDatabase;
 10 import android.view.View;
 11 import android.widget.SimpleCursorAdapter;
 12 import android.widget.TextView;
 13 import cn.yj3g.student.activity.R;
 14 import cn.yj3g.student.db.StudentDBHelper;
 15 import cn.yj3g.student.entry.Student;
 16 import cn.yj3g.student.entry.TableContanst;
 17 
 18 public class StudentDao {
 19 
 20     private StudentDBHelper dbHelper;
 21     private Cursor cursor;
 22     public StudentDao(StudentDBHelper dbHelper) {
 23         this.dbHelper = dbHelper;
 24     }
 25     // 添加一个Student对象数据到数据库表
 26     public long addStudent(Student s) {
 27 
 28         ContentValues values = new ContentValues();
 29         values.put(TableContanst.StudentColumns.NAME, s.getName());
 30         values.put(TableContanst.StudentColumns.AGE, s.getAge());
 31         values.put(TableContanst.StudentColumns.SEX, s.getSex());
 32         values.put(TableContanst.StudentColumns.LIKES, s.getLike());
 33         values.put(TableContanst.StudentColumns.PHONE_NUMBER, s.getPhoneNumber());
 34         values.put(TableContanst.StudentColumns.TRAIN_DATE, s.getTrainDate());
 35         values.put(TableContanst.StudentColumns.MODIFY_TIME, s.getModifyDateTime());
 36         return dbHelper.getWritableDatabase().insert(TableContanst.STUDENT_TABLE, null, values);
 37 
 38     }
 39 
 40     // 删除一个id所对应的数据库表student的记录
 41     public int deleteStudentById(long id) {
 42 
 43         return dbHelper.getWritableDatabase().delete(TableContanst.STUDENT_TABLE,
 44                 TableContanst.StudentColumns.ID + "=?", new String[] { id + "" });
 45     }
 46 
 47     // 更新一个id所对应数据库表student的记录
 48     public int updateStudent(Student s) {
 49 
 50         ContentValues values = new ContentValues();
 51         values.put(TableContanst.StudentColumns.NAME, s.getName());
 52         values.put(TableContanst.StudentColumns.AGE, s.getAge());
 53         values.put(TableContanst.StudentColumns.SEX, s.getSex());
 54         values.put(TableContanst.StudentColumns.LIKES, s.getLike());
 55         values.put(TableContanst.StudentColumns.PHONE_NUMBER, s.getPhoneNumber());
 56         values.put(TableContanst.StudentColumns.TRAIN_DATE, s.getTrainDate());
 57         values.put(TableContanst.StudentColumns.MODIFY_TIME, s.getModifyDateTime());
 58         return dbHelper.getWritableDatabase().update(TableContanst.STUDENT_TABLE, values,
 59                 TableContanst.StudentColumns.ID + "=?", new String[] { s.getId() + "" });
 60 
 61     }
 62 
 63     // 查询所有的记录
 64     public List<Map<String,Object>> getAllStudents() { //modify_time desc
 65         
 66         List<Map<String, Object>> data = new ArrayList<Map<String,Object>>();
 67         Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE, null, null, null,
 68                 null, null, TableContanst.StudentColumns.MODIFY_TIME+" desc");
 69         while(cursor.moveToNext()) {
 70             Map<String, Object> map = new HashMap<String, Object>(8);
 71             long id = cursor.getInt(cursor.getColumnIndex(TableContanst.StudentColumns.ID));
 72             map.put(TableContanst.StudentColumns.ID, id);
 73             String name = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.NAME));
 74             map.put(TableContanst.StudentColumns.NAME, name);
 75             int age = cursor.getInt(cursor.getColumnIndex(TableContanst.StudentColumns.AGE));
 76             map.put(TableContanst.StudentColumns.AGE, age);
 77             String sex = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.SEX));
 78             map.put(TableContanst.StudentColumns.SEX, sex);
 79             String likes = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.LIKES));
 80             map.put(TableContanst.StudentColumns.LIKES, likes);
 81             String phone_number = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.PHONE_NUMBER));
 82             map.put(TableContanst.StudentColumns.PHONE_NUMBER, phone_number);
 83             String train_date = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.TRAIN_DATE));
 84             map.put(TableContanst.StudentColumns.TRAIN_DATE, train_date);
 85             String modify_time = cursor.getString(cursor.getColumnIndex(TableContanst.StudentColumns.MODIFY_TIME));
 86             map.put(TableContanst.StudentColumns.MODIFY_TIME, modify_time);
 87             data.add(map);
 88         }
 89         return data;
 90     }
 91     //模糊查询一条记录
 92     public Cursor findStudent(String name){
 93     
 94         Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE,  null, "name like ?",
 95                 new String[] { "%" + name + "%" }, null, null, null,null);
 96     return cursor;
 97     }
 98     //按姓名进行排序
 99     public Cursor sortByName(){
100         Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE,  null,null,
101                 null, null, null,TableContanst.StudentColumns.NAME);
102     return cursor;
103     }
104     //按入学日期进行排序
105     public Cursor sortByTrainDate(){
106         Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE,  null,null,
107                 null, null, null,TableContanst.StudentColumns.TRAIN_DATE);
108     return cursor;
109     }
110     //按学号进行排序
111     public Cursor sortByID(){
112         Cursor cursor = dbHelper.getWritableDatabase().query(TableContanst.STUDENT_TABLE,  null,null,
113                 null, null, null,TableContanst.StudentColumns.ID);
114     return cursor;
115     }
116     public void closeDB() {
117         dbHelper.close();
118     }
119     //自定义的方法通过View和Id得到一个student对象
120     public Student getStudentFromView(View view, long id) {
121         TextView nameView = (TextView) view.findViewById(R.id.tv_stu_name);
122         TextView ageView = (TextView) view.findViewById(R.id.tv_stu_age);
123         TextView sexView = (TextView) view.findViewById(R.id.tv_stu_sex);
124         TextView likeView = (TextView) view.findViewById(R.id.tv_stu_likes);
125         TextView phoneView = (TextView) view.findViewById(R.id.tv_stu_phone);
126         TextView dataView = (TextView) view.findViewById(R.id.tv_stu_traindate);
127         String name = nameView.getText().toString();
128         int age = Integer.parseInt(ageView.getText().toString());
129         String sex = sexView.getText().toString();
130         String like = likeView.getText().toString();
131         String phone = phoneView.getText().toString();
132         String data = dataView.getText().toString();
133         Student student = new Student(id, name, age, sex, like, phone, data,null);
134         return student;
135     }
136     
137 }
复制代码

StudentDBHelper类

复制代码
 1 package cn.yj3g.student.db;
 2 
 3 import cn.yj3g.student.entry.TableContanst;
 4 import android.content.Context;
 5 import android.database.sqlite.SQLiteDatabase;
 6 import android.database.sqlite.SQLiteDatabase.CursorFactory;
 7 import android.database.sqlite.SQLiteOpenHelper;
 8 import android.util.Log;
 9 
10 public class StudentDBHelper extends SQLiteOpenHelper {
11 
12     private static final String TAG = "StudentDBHelper";
13 
14     public static final String DB_NAME = "student_manager.db";
15     public static final int VERSION = 1;
16     //构造方法
17     public StudentDBHelper(Context context, String name, CursorFactory factory, int version) {
18         super(context, name, factory, version);
19 
20     }
21 
22     public StudentDBHelper(Context context) {
23         this(context, DB_NAME, null, VERSION);
24     }
25     //创建数据库
26     @Override
27     public void onCreate(SQLiteDatabase db) {
28         Log.v(TAG, "onCreate");
29         db.execSQL("create table "
30                 + TableContanst.STUDENT_TABLE
31                 + "(_id Integer primary key AUTOINCREMENT,"
32                 + "name char,age integer, sex char, likes char, phone_number char,train_date date, "
33                 + "modify_time DATETIME)");
34     }
35     //更新数据库
36     @Override
37     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
38         Log.v(TAG, "onUpgrade");
39     }
40 
41 }
复制代码

Student类:

复制代码
  1 package cn.yj3g.student.entry;
  2 
  3 import java.io.Serializable;
  4 
  5 import android.view.View;
  6 import android.widget.TextView;
  7 import cn.yj3g.student.activity.R;
  8 
  9 public class Student implements Serializable{
 10 
 11     private long id;
 12     private String name;
 13     private int age;
 14     private String sex;
 15     private String like;
 16     private String phoneNumber;
 17     private String trainDate;
 18     private String modifyDateTime;
 19     
 20     
 21     public Student() {
 22         super();
 23     }
 24 
 25     public Student(long id, String name, int age, String sex, String like, String phoneNumber,
 26             String trainDate, String modifyDateTime) {
 27         super();
 28         this.id = id;
 29         this.name = name;
 30         this.age = age;
 31         this.sex = sex;
 32         this.like = like;
 33         this.phoneNumber = phoneNumber;
 34         this.trainDate = trainDate;
 35         this.modifyDateTime = modifyDateTime;
 36     }
 37     
 38     public Student(String name, int age, String sex, String like, String phoneNumber,
 39             String trainDate, String modifyDateTime) {
 40         super();
 41         this.name = name;
 42         this.age = age;
 43         this.sex = sex;
 44         this.like = like;
 45         this.phoneNumber = phoneNumber;
 46         this.trainDate = trainDate;
 47         this.modifyDateTime = modifyDateTime;
 48     }
 49 
 50     public long getId() {
 51         return id;
 52     }
 53     public void setId(long id) {
 54         this.id = id;
 55     }
 56     public String getName() {
 57         return name;
 58     }
 59     public void setName(String name) {
 60         this.name = name;
 61     }
 62     public int getAge() {
 63         return age;
 64     }
 65     public void setAge(int age) {
 66         this.age = age;
 67     }
 68     public String getSex() {
 69         return sex;
 70     }
 71     public void setSex(String sex) {
 72         this.sex = sex;
 73     }
 74     public String getLike() {
 75         return like;
 76     }
 77     public void setLike(String like) {
 78         this.like = like;
 79     }
 80     public String getPhoneNumber() {
 81         return phoneNumber;
 82     }
 83     public void setPhoneNumber(String phoneNumber) {
 84         this.phoneNumber = phoneNumber;
 85     }
 86     public String getTrainDate() {
 87         return trainDate;
 88     }
 89     public void setTrainDate(String trainDate) {
 90         this.trainDate = trainDate;
 91     }
 92     public String getModifyDateTime() {
 93         return modifyDateTime;
 94     }
 95     public void setModifyDateTime(String modifyDateTime) {
 96         this.modifyDateTime = modifyDateTime;
 97     }
 98     @Override
 99     public int hashCode() {
100         final int prime = 31;
101         int result = 1;
102         result = prime * result + age;
103         result = prime * result + (int) (id ^ (id >>> 32));
104         result = prime * result + ((like == null) ? 0 : like.hashCode());
105         result = prime * result + ((modifyDateTime == null) ? 0 : modifyDateTime.hashCode());
106         result = prime * result + ((name == null) ? 0 : name.hashCode());
107         result = prime * result + ((phoneNumber == null) ? 0 : phoneNumber.hashCode());
108         result = prime * result + ((sex == null) ? 0 : sex.hashCode());
109         result = prime * result + ((trainDate == null) ? 0 : trainDate.hashCode());
110         return result;
111     }
112     @Override
113     public boolean equals(Object obj) {
114         if (this == obj)
115             return true;
116         if (obj == null)
117             return false;
118         if (getClass() != obj.getClass())
119             return false;
120         Student other = (Student) obj;
121         if (age != other.age)
122             return false;
123         if (id != other.id)
124             return false;
125         if (like == null) {
126             if (other.like != null)
127                 return false;
128         } else if (!like.equals(other.like))
129             return false;
130         if (modifyDateTime == null) {
131             if (other.modifyDateTime != null)
132                 return false;
133         } else if (!modifyDateTime.equals(other.modifyDateTime))
134             return false;
135         if (name == null) {
136             if (other.name != null)
137                 return false;
138         } else if (!name.equals(other.name))
139             return false;
140         if (phoneNumber == null) {
141             if (other.phoneNumber != null)
142                 return false;
143         } else if (!phoneNumber.equals(other.phoneNumber))
144             return false;
145         if (sex == null) {
146             if (other.sex != null)
147                 return false;
148         } else if (!sex.equals(other.sex))
149             return false;
150         if (trainDate == null) {
151             if (other.trainDate != null)
152                 return false;
153         } else if (!trainDate.equals(other.trainDate))
154             return false;
155         return true;
156     }
157     @Override
158     public String toString() {
159         return "Student [id=" + id + ", name=" + name + "]";
160     }
161 }
复制代码

常量TableContanst类:

复制代码
 1 package cn.yj3g.student.entry;
 2 
 3 public final class TableContanst {
 4     
 5     public static final String STUDENT_TABLE = "student";
 6     
 7     public static final class StudentColumns {
 8         public static final String ID = "_id";
 9         public static final String NAME = "name";
10         public static final String AGE = "age";
11         public static final String SEX = "sex";
12         public static final String LIKES = "likes";
13         public static final String PHONE_NUMBER = "phone_number";
14         public static final String TRAIN_DATE = "train_date";
15         public static final String MODIFY_TIME = "modify_time";
16     }
17 }
复制代码

最后我将我的values里的strings.xml和AndroidManifest.xml附在下面,以供大家参考。

strings.xml:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3     <string name="hello">Hello World, TextStudentManager!</string>
 4     <string name="app_name">学员管理系统</string>
 5     <string name="information_write">学员信息修改</string>
 6     <string name="button1"></string>
 7     <string name="button2"></string>
 8     <string name="box1">唱歌</string>
 9     <string name="box2">跳舞</string>
10     <string name="box3">健身</string>
11     <string name="myButton">添加学员信息</string>
12     <string name="spinner">请选择</string>    
13 </resources>
复制代码

AndroidManifest.xml:

复制代码
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 3       package="cn.yj3g.student.activity"
 4       android:versionCode="1"
 5       android:versionName="1.0">
 6     <uses-sdk android:minSdkVersion="8" />
 7 
 8     <application android:icon="@drawable/student"  android:label="@string/app_name">
 9         <activity android:name=".StudentListActivity"
10                   android:label="学员信息列表">
11             <intent-filter>
12                 <action android:name="android.intent.action.MAIN" />
13                 <category android:name="android.intent.category.LAUNCHER" />
14             </intent-filter>
15         </activity>
16         
17          <activity android:name=".AddStudentActivity"
18                   android:label="学员信息添加">
19         </activity>
20           <activity android:name=".ShowStudentActivity"
21                   android:label="学员详细信息">
22         </activity>
23          <activity android:name=".StudentSearch"
24                   android:label="学员信息查询">
25         </activity>
26          <activity android:name=".SearchResult"
27                   android:label="查询结果">
28         </activity>
29         <provider 
30         android:name=".MyStudentManagerProvider"
31         android:authorities="cn.yj3g.student.activity.MyStudentManagerProvider"></provider>
32         <uses-library android:name="android.test.runner" />  <!-- android测试包 -->
33         
34     </application>
35     <uses-permission android:name="android.permission.READ_CONTACTS"></uses-permission> 
36     <instrumentation android:name="android.test.InstrumentationTestRunner"
37           android:targetPackage="cn.yj3g.student.activity" android:label="Tests for My App" />
38 </manifest>
复制代码


由于自己也是初学者,所以大家发现什么问题,可以留言,大家一起学习。

查看全文
如若内容造成侵权/违法违规/事实不符,请联系编程学习网邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!

相关文章

  1. 关于易宝支付的回调和跨域请求

    此次开发是基于小京东商城的开发(ecshop)1.在支付的SDK里面有个call_back文件,支付的文档里面有个notify参数,里面的地址就写成能访问到call_back.php文件就行,切记,一定要是能访问到此地址2.易宝支付的服务器会发送一个response参数还有另外一个参数,忽略,response参数里…...

    2024/5/1 14:46:28
  2. nginx负载均衡

    应用场景 当一个应用部署在tomcat后,发现访问量越来越多,服务器完全无法承受住压力,导致系统卡顿延时等,此时需要通过nginx技术将应用压力进行平均分配到其他服务器,将多台服务器一起提供资源,通过nginx来协调资源进行负载均衡。 操作步骤 1. 目标与准备 目标:使用docke…...

    2024/4/24 15:04:15
  3. 任务创建函数OSTaskCreate解析 .

    任务是操作系统处理的首要对象,在多任务运行的环境中,任务的管理需要考虑多方面的因素,最基本的任务管理是任务的创建。任务创建函数有两种,一种是基本的创建函数OSTaskCreate,另一种是扩展的任务创建函数OSTaskCreateExt。两个函数都实现了任务的创建,但是OSTaskCreateE…...

    2024/4/24 15:04:15
  4. 配置Nginx gzip压缩功能

    # 配置Nginx gzip压缩功能 #要压缩的内容(js,css,html...),不要压缩的内容(图片,视频,flash..) 提供了对文件内容压缩的功能,允许将输出内容在发送到客户端之前根据具体的策略压缩节约贷带宽。功能同apache的mod_deflate压缩功能。依赖ngx_http_gzip_module模块。默认…...

    2024/4/24 15:04:13
  5. Ureport2指自定义名称

    如:/ureport/excel?_n=王瑜&_u=file:DhMreportstationgpsSum.ureport.xml...

    2024/4/24 15:04:12
  6. BroadcastReceiver

    定义 即广播,是一个全局的监听器,属于Android四大组件之一,有两个角色:广播发送者、广播接收者;它的作用是监听/接收应用App发出的广播消息,并作出响应 应用场景 Android不同组件间的通信(应用内/不同应用之间) 多线程通信 与Android系统在特定情况下的通信 实现原理 采…...

    2024/4/24 15:04:11
  7. 【181218】VC++学生考试管理系统Access版源代码

    源码下载简介vc++简易学生考试管理系统,分两大功能,学生成绩管理和学生信息管理,功能不太多,完成基本的学生信息录入、查询、管理,可以先看一下 测试抓图,比较标准的C++窗体应用,就当作是一个VC操作数据库的参考实例 吧。源码下载地址:点击下载备用下载地址:点击下载...

    2024/4/24 15:04:10
  8. Day09+10流相关内容

    Day09+Day10 一、IO ​ IO分别代表Input输入,和Output输出。数据的传输需要借助流,可以理解为数据的流动。根据流动的方向不同,分为输入流(由设备到内存)和输出流(由内存到设备)。 根据传输的数据类型不同,分为字节流和字符流。流的顶级父类:顶级父类 输入流 输出流字…...

    2024/4/24 15:04:10
  9. springboot整合UReport2

    1、首先新建一个springboot项目可以用idea直接新建,也可以在spring-boot官方提供的生成器生成项目,生成地址是:https://start.spring.io/2、配置pom.xml<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spr…...

    2024/4/24 15:04:08
  10. 网站启用GZip压缩后,速度快了3倍!

    GZip压缩,是一种网站速度优化技术,也是一把SEO优化利器,许多网站都采用了这种技术,以达到提升网页打开速度、缩短网页打开时间的目的。本文是一篇研究性文章,将通过某个案例来给大家显示网站采用GZip压缩前后的对比效果。这里将要研究的对象是园子的博客http://www.yzznl.…...

    2024/4/23 23:51:09
  11. Google Maglev 牛逼的网络负载均衡器

    Maglev 是什么Maglev 是谷歌搞的一个工作在三层(IP层)的网络负载均衡器, 它是一个运行在普通的 Linux 系统上的巨大的分布式系统, 并且可以简单平滑的伸缩后端服务器数量, 谷歌在自己的数据中心便使用该方案做负载均衡, 后面又以论文的形式将 Maglev 的负载均衡方案分享了出来.…...

    2024/4/24 15:04:06
  12. OSTaskCreate创建失败原因之一

    在μcos中调用这个任务创建函数发现失败,debug进去,发现在err = OS_TCBInit(prio, psp, (OS_STK *)0, 0u, 0u, (void *)0, 0u);后err = OS_ERR_TASK_NO_MORE_TCB这是因为在创建任务之初没有调用OSInit();...

    2024/4/24 15:04:05
  13. 【Java EE 学习第21天 使用易宝支付接口实现java网上支付功能】

    转载自:http://www.w2bc.com/article/46114 不全但是挺详细...

    2024/4/26 20:04:58
  14. cloud-config+bus+gitlab+security实现配置文件动态刷新

    文章目录cloud-config+bus+gitlab+security实现配置文件动态刷新一、配置中心服务端1.1、pom.xml1.2、yml文件1.3、主入口1.4、SecurityConfig1.5、过滤器二、配置中心客户端2.1、pom.xml2.2、bootstrap.yml1.3、git里面的配置文件要有三、gitLab的web钩子 cloud-config+bus+gi…...

    2024/4/24 15:04:03
  15. L4和L7负载均衡原理和常用负载均衡架构实现

    L4和L7负载均衡原理四层负载均衡原理所谓四层负载均衡,也就是主要通过报文中的目标地址和端口,再加上负载均衡设备设置的服务器选择方式,决定最终选择的内部服务器。以常见的TCP为例,负载均衡设备在接收到第一个来自客户端的SYN请求时,即通过上述方式选择一个最佳的服务器…...

    2024/4/24 15:04:02
  16. Grails4 整合Ureport2报表

    1、build.gradle引入组件// https://mvnrepository.com/artifact/com.bstek.ureport/ureport2-console compile group: com.bstek.ureport, name: ureport2-console, version: 2.2.92、在spring/resources.groovy中注入beanimport com.bstek.ureport.console.UReportServlet im…...

    2024/4/24 15:04:01
  17. apache开启压缩功能

    mod_deflate和mod_gzip 所谓gzip,其实在早期的apache 1.x系列版本中没有内建网页压缩技术,所以才需要去gzip压缩,apache2官方在开发的时候,就已经把网页压缩考虑进去,内建了mod_deflate模块,所以apache2就不需要使用到mod_gzip了,这两者的工作原理是类似的,还有启用mod…...

    2024/4/24 15:04:00
  18. 笔记之uC/OS 多任务机制OSTaskCreate()

    想让uC/OS-Ⅱ管理用户的任务,用户必须要先建立任务。用户可以通过传递任务地址和其它参数到以下两个函数之一来建立任务:OSTaskCreate() 或 OSTaskCreateExt()。OSTaskCreate()与uC/OS是向下兼容的,OSTaskCreateExt()是OSTaskCreate()的扩展版本,提供了一些附加的功能。用两…...

    2024/4/19 16:13:54
  19. java学生管理系统代码

    //导入的包名import java.io.BufferedReader;import java.io.BufferedWriter;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.util.ArrayList;import java.util.Scanner; //学生管理系统类public class StudentManager {public …...

    2024/4/15 2:57:00
  20. 在线支付(易宝支付)

    第三方支付方式:易宝支付。支付过程图解:相关资料:易宝支付产品通用接口帮助文档点击打开链接使用步骤: ①src下面放入支付测试用的merchantInfo.properties(包含商家id【p1_MerId=10001126856】加密解密用的keyValue【keyValue=69cl522AV6q613Ii4W6u8K6XuW8vM1N6bFgyv…...

    2024/4/20 6:35:55

最新文章

  1. Swagger3.0(Springdoc)日常使用记录

    文章目录 前言一、默认地址二、注解OperationTag 三、SpringBoot基础配置四、Swagger导入apifox五、Swagger其他配置六 knife4j 参考文章 前言 本文并不是Swagger的使用教程&#xff0c;只是记录一下本人的操作&#xff0c;感兴趣的可以看下 一、默认地址 http://localhost:…...

    2024/5/1 16:57:14
  2. 梯度消失和梯度爆炸的一些处理方法

    在这里是记录一下梯度消失或梯度爆炸的一些处理技巧。全当学习总结了如有错误还请留言&#xff0c;在此感激不尽。 权重和梯度的更新公式如下&#xff1a; w w − η ⋅ ∇ w w w - \eta \cdot \nabla w ww−η⋅∇w 个人通俗的理解梯度消失就是网络模型在反向求导的时候出…...

    2024/3/20 10:50:27
  3. composer常见错误解决

    在Java中&#xff0c;常见的问题和解决方法包括&#xff1a; 内存不足错误&#xff1a;Java应用程序在运行时可能会遇到内存不足的错误。可以通过增加JVM的堆内存大小来解决&#xff0c;可以通过设置-Xms和-Xmx参数来指定初始堆大小和最大堆大小。 java -Xms2G -Xmx4G YourAppl…...

    2024/4/30 3:27:03
  4. [C++/Linux] UDP编程

    一. UDP函数 UDP&#xff08;用户数据报协议&#xff0c;User Datagram Protocol&#xff09;是一种无连接的网络协议&#xff0c;用于在互联网上交换数据。它允许应用程序发送数据报给另一端的应用程序&#xff0c;但不保证数据报能成功到达&#xff0c;也就是说&#xff0c;它…...

    2024/4/30 4:30:51
  5. 【外汇早评】美通胀数据走低,美元调整

    原标题:【外汇早评】美通胀数据走低,美元调整昨日美国方面公布了新一期的核心PCE物价指数数据,同比增长1.6%,低于前值和预期值的1.7%,距离美联储的通胀目标2%继续走低,通胀压力较低,且此前美国一季度GDP初值中的消费部分下滑明显,因此市场对美联储后续更可能降息的政策…...

    2024/4/29 23:16:47
  6. 【原油贵金属周评】原油多头拥挤,价格调整

    原标题:【原油贵金属周评】原油多头拥挤,价格调整本周国际劳动节,我们喜迎四天假期,但是整个金融市场确实流动性充沛,大事频发,各个商品波动剧烈。美国方面,在本周四凌晨公布5月份的利率决议和新闻发布会,维持联邦基金利率在2.25%-2.50%不变,符合市场预期。同时美联储…...

    2024/4/30 18:14:14
  7. 【外汇周评】靓丽非农不及疲软通胀影响

    原标题:【外汇周评】靓丽非农不及疲软通胀影响在刚结束的周五,美国方面公布了新一期的非农就业数据,大幅好于前值和预期,新增就业重新回到20万以上。具体数据: 美国4月非农就业人口变动 26.3万人,预期 19万人,前值 19.6万人。 美国4月失业率 3.6%,预期 3.8%,前值 3…...

    2024/4/29 2:29:43
  8. 【原油贵金属早评】库存继续增加,油价收跌

    原标题:【原油贵金属早评】库存继续增加,油价收跌周三清晨公布美国当周API原油库存数据,上周原油库存增加281万桶至4.692亿桶,增幅超过预期的74.4万桶。且有消息人士称,沙特阿美据悉将于6月向亚洲炼油厂额外出售更多原油,印度炼油商预计将每日获得至多20万桶的额外原油供…...

    2024/4/30 18:21:48
  9. 【外汇早评】日本央行会议纪要不改日元强势

    原标题:【外汇早评】日本央行会议纪要不改日元强势近两日日元大幅走强与近期市场风险情绪上升,避险资金回流日元有关,也与前一段时间的美日贸易谈判给日本缓冲期,日本方面对汇率问题也避免继续贬值有关。虽然今日早间日本央行公布的利率会议纪要仍然是支持宽松政策,但这符…...

    2024/4/27 17:58:04
  10. 【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响

    原标题:【原油贵金属早评】欧佩克稳定市场,填补伊朗问题的影响近日伊朗局势升温,导致市场担忧影响原油供给,油价试图反弹。此时OPEC表态稳定市场。据消息人士透露,沙特6月石油出口料将低于700万桶/日,沙特已经收到石油消费国提出的6月份扩大出口的“适度要求”,沙特将满…...

    2024/4/27 14:22:49
  11. 【外汇早评】美欲与伊朗重谈协议

    原标题:【外汇早评】美欲与伊朗重谈协议美国对伊朗的制裁遭到伊朗的抗议,昨日伊朗方面提出将部分退出伊核协议。而此行为又遭到欧洲方面对伊朗的谴责和警告,伊朗外长昨日回应称,欧洲国家履行它们的义务,伊核协议就能保证存续。据传闻伊朗的导弹已经对准了以色列和美国的航…...

    2024/4/28 1:28:33
  12. 【原油贵金属早评】波动率飙升,市场情绪动荡

    原标题:【原油贵金属早评】波动率飙升,市场情绪动荡因中美贸易谈判不安情绪影响,金融市场各资产品种出现明显的波动。随着美国与中方开启第十一轮谈判之际,美国按照既定计划向中国2000亿商品征收25%的关税,市场情绪有所平复,已经开始接受这一事实。虽然波动率-恐慌指数VI…...

    2024/4/30 9:43:09
  13. 【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试

    原标题:【原油贵金属周评】伊朗局势升温,黄金多头跃跃欲试美国和伊朗的局势继续升温,市场风险情绪上升,避险黄金有向上突破阻力的迹象。原油方面稍显平稳,近期美国和OPEC加大供给及市场需求回落的影响,伊朗局势并未推升油价走强。近期中美贸易谈判摩擦再度升级,美国对中…...

    2024/4/27 17:59:30
  14. 【原油贵金属早评】市场情绪继续恶化,黄金上破

    原标题:【原油贵金属早评】市场情绪继续恶化,黄金上破周初中国针对于美国加征关税的进行的反制措施引发市场情绪的大幅波动,人民币汇率出现大幅的贬值动能,金融市场受到非常明显的冲击。尤其是波动率起来之后,对于股市的表现尤其不安。隔夜美国股市出现明显的下行走势,这…...

    2024/4/25 18:39:16
  15. 【外汇早评】美伊僵持,风险情绪继续升温

    原标题:【外汇早评】美伊僵持,风险情绪继续升温昨日沙特两艘油轮再次发生爆炸事件,导致波斯湾局势进一步恶化,市场担忧美伊可能会出现摩擦生火,避险品种获得支撑,黄金和日元大幅走强。美指受中美贸易问题影响而在低位震荡。继5月12日,四艘商船在阿联酋领海附近的阿曼湾、…...

    2024/4/28 1:34:08
  16. 【原油贵金属早评】贸易冲突导致需求低迷,油价弱势

    原标题:【原油贵金属早评】贸易冲突导致需求低迷,油价弱势近日虽然伊朗局势升温,中东地区几起油船被袭击事件影响,但油价并未走高,而是出于调整结构中。由于市场预期局势失控的可能性较低,而中美贸易问题导致的全球经济衰退风险更大,需求会持续低迷,因此油价调整压力较…...

    2024/4/26 19:03:37
  17. 氧生福地 玩美北湖(上)——为时光守候两千年

    原标题:氧生福地 玩美北湖(上)——为时光守候两千年一次说走就走的旅行,只有一张高铁票的距离~ 所以,湖南郴州,我来了~ 从广州南站出发,一个半小时就到达郴州西站了。在动车上,同时改票的南风兄和我居然被分到了一个车厢,所以一路非常愉快地聊了过来。 挺好,最起…...

    2024/4/29 20:46:55
  18. 氧生福地 玩美北湖(中)——永春梯田里的美与鲜

    原标题:氧生福地 玩美北湖(中)——永春梯田里的美与鲜一觉醒来,因为大家太爱“美”照,在柳毅山庄去寻找龙女而错过了早餐时间。近十点,向导坏坏还是带着饥肠辘辘的我们去吃郴州最富有盛名的“鱼头粉”。说这是“十二分推荐”,到郴州必吃的美食之一。 哇塞!那个味美香甜…...

    2024/4/30 22:21:04
  19. 氧生福地 玩美北湖(下)——奔跑吧骚年!

    原标题:氧生福地 玩美北湖(下)——奔跑吧骚年!让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 让我们红尘做伴 活得潇潇洒洒 策马奔腾共享人世繁华 对酒当歌唱出心中喜悦 轰轰烈烈把握青春年华 啊……啊……啊 两…...

    2024/5/1 4:32:01
  20. 扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!

    原标题:扒开伪装医用面膜,翻六倍价格宰客,小姐姐注意了!扒开伪装医用面膜,翻六倍价格宰客!当行业里的某一品项火爆了,就会有很多商家蹭热度,装逼忽悠,最近火爆朋友圈的医用面膜,被沾上了污点,到底怎么回事呢? “比普通面膜安全、效果好!痘痘、痘印、敏感肌都能用…...

    2024/4/27 23:24:42
  21. 「发现」铁皮石斛仙草之神奇功效用于医用面膜

    原标题:「发现」铁皮石斛仙草之神奇功效用于医用面膜丽彦妆铁皮石斛医用面膜|石斛多糖无菌修护补水贴19大优势: 1、铁皮石斛:自唐宋以来,一直被列为皇室贡品,铁皮石斛生于海拔1600米的悬崖峭壁之上,繁殖力差,产量极低,所以古代仅供皇室、贵族享用 2、铁皮石斛自古民间…...

    2024/4/28 5:48:52
  22. 丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者

    原标题:丽彦妆\医用面膜\冷敷贴轻奢医学护肤引导者【公司简介】 广州华彬企业隶属香港华彬集团有限公司,专注美业21年,其旗下品牌: 「圣茵美」私密荷尔蒙抗衰,产后修复 「圣仪轩」私密荷尔蒙抗衰,产后修复 「花茵莳」私密荷尔蒙抗衰,产后修复 「丽彦妆」专注医学护…...

    2024/4/30 9:42:22
  23. 广州械字号面膜生产厂家OEM/ODM4项须知!

    原标题:广州械字号面膜生产厂家OEM/ODM4项须知!广州械字号面膜生产厂家OEM/ODM流程及注意事项解读: 械字号医用面膜,其实在我国并没有严格的定义,通常我们说的医美面膜指的应该是一种「医用敷料」,也就是说,医用面膜其实算作「医疗器械」的一种,又称「医用冷敷贴」。 …...

    2024/4/30 9:43:22
  24. 械字号医用眼膜缓解用眼过度到底有无作用?

    原标题:械字号医用眼膜缓解用眼过度到底有无作用?医用眼膜/械字号眼膜/医用冷敷眼贴 凝胶层为亲水高分子材料,含70%以上的水分。体表皮肤温度传导到本产品的凝胶层,热量被凝胶内水分子吸收,通过水分的蒸发带走大量的热量,可迅速地降低体表皮肤局部温度,减轻局部皮肤的灼…...

    2024/4/30 9:42:49
  25. 配置失败还原请勿关闭计算机,电脑开机屏幕上面显示,配置失败还原更改 请勿关闭计算机 开不了机 这个问题怎么办...

    解析如下&#xff1a;1、长按电脑电源键直至关机&#xff0c;然后再按一次电源健重启电脑&#xff0c;按F8健进入安全模式2、安全模式下进入Windows系统桌面后&#xff0c;按住“winR”打开运行窗口&#xff0c;输入“services.msc”打开服务设置3、在服务界面&#xff0c;选中…...

    2022/11/19 21:17:18
  26. 错误使用 reshape要执行 RESHAPE,请勿更改元素数目。

    %读入6幅图像&#xff08;每一幅图像的大小是564*564&#xff09; f1 imread(WashingtonDC_Band1_564.tif); subplot(3,2,1),imshow(f1); f2 imread(WashingtonDC_Band2_564.tif); subplot(3,2,2),imshow(f2); f3 imread(WashingtonDC_Band3_564.tif); subplot(3,2,3),imsho…...

    2022/11/19 21:17:16
  27. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机...

    win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”问题的解决方法在win7系统关机时如果有升级系统的或者其他需要会直接进入一个 等待界面&#xff0c;在等待界面中我们需要等待操作结束才能关机&#xff0c;虽然这比较麻烦&#xff0c;但是对系统进行配置和升级…...

    2022/11/19 21:17:15
  28. 台式电脑显示配置100%请勿关闭计算机,“准备配置windows 请勿关闭计算机”的解决方法...

    有不少用户在重装Win7系统或更新系统后会遇到“准备配置windows&#xff0c;请勿关闭计算机”的提示&#xff0c;要过很久才能进入系统&#xff0c;有的用户甚至几个小时也无法进入&#xff0c;下面就教大家这个问题的解决方法。第一种方法&#xff1a;我们首先在左下角的“开始…...

    2022/11/19 21:17:14
  29. win7 正在配置 请勿关闭计算机,怎么办Win7开机显示正在配置Windows Update请勿关机...

    置信有很多用户都跟小编一样遇到过这样的问题&#xff0c;电脑时发现开机屏幕显现“正在配置Windows Update&#xff0c;请勿关机”(如下图所示)&#xff0c;而且还需求等大约5分钟才干进入系统。这是怎样回事呢&#xff1f;一切都是正常操作的&#xff0c;为什么开时机呈现“正…...

    2022/11/19 21:17:13
  30. 准备配置windows 请勿关闭计算机 蓝屏,Win7开机总是出现提示“配置Windows请勿关机”...

    Win7系统开机启动时总是出现“配置Windows请勿关机”的提示&#xff0c;没过几秒后电脑自动重启&#xff0c;每次开机都这样无法进入系统&#xff0c;此时碰到这种现象的用户就可以使用以下5种方法解决问题。方法一&#xff1a;开机按下F8&#xff0c;在出现的Windows高级启动选…...

    2022/11/19 21:17:12
  31. 准备windows请勿关闭计算机要多久,windows10系统提示正在准备windows请勿关闭计算机怎么办...

    有不少windows10系统用户反映说碰到这样一个情况&#xff0c;就是电脑提示正在准备windows请勿关闭计算机&#xff0c;碰到这样的问题该怎么解决呢&#xff0c;现在小编就给大家分享一下windows10系统提示正在准备windows请勿关闭计算机的具体第一种方法&#xff1a;1、2、依次…...

    2022/11/19 21:17:11
  32. 配置 已完成 请勿关闭计算机,win7系统关机提示“配置Windows Update已完成30%请勿关闭计算机”的解决方法...

    今天和大家分享一下win7系统重装了Win7旗舰版系统后&#xff0c;每次关机的时候桌面上都会显示一个“配置Windows Update的界面&#xff0c;提示请勿关闭计算机”&#xff0c;每次停留好几分钟才能正常关机&#xff0c;导致什么情况引起的呢&#xff1f;出现配置Windows Update…...

    2022/11/19 21:17:10
  33. 电脑桌面一直是清理请关闭计算机,windows7一直卡在清理 请勿关闭计算机-win7清理请勿关机,win7配置更新35%不动...

    只能是等着&#xff0c;别无他法。说是卡着如果你看硬盘灯应该在读写。如果从 Win 10 无法正常回滚&#xff0c;只能是考虑备份数据后重装系统了。解决来方案一&#xff1a;管理员运行cmd&#xff1a;net stop WuAuServcd %windir%ren SoftwareDistribution SDoldnet start WuA…...

    2022/11/19 21:17:09
  34. 计算机配置更新不起,电脑提示“配置Windows Update请勿关闭计算机”怎么办?

    原标题&#xff1a;电脑提示“配置Windows Update请勿关闭计算机”怎么办&#xff1f;win7系统中在开机与关闭的时候总是显示“配置windows update请勿关闭计算机”相信有不少朋友都曾遇到过一次两次还能忍但经常遇到就叫人感到心烦了遇到这种问题怎么办呢&#xff1f;一般的方…...

    2022/11/19 21:17:08
  35. 计算机正在配置无法关机,关机提示 windows7 正在配置windows 请勿关闭计算机 ,然后等了一晚上也没有关掉。现在电脑无法正常关机...

    关机提示 windows7 正在配置windows 请勿关闭计算机 &#xff0c;然后等了一晚上也没有关掉。现在电脑无法正常关机以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;关机提示 windows7 正在配…...

    2022/11/19 21:17:05
  36. 钉钉提示请勿通过开发者调试模式_钉钉请勿通过开发者调试模式是真的吗好不好用...

    钉钉请勿通过开发者调试模式是真的吗好不好用 更新时间:2020-04-20 22:24:19 浏览次数:729次 区域: 南阳 > 卧龙 列举网提醒您:为保障您的权益,请不要提前支付任何费用! 虚拟位置外设器!!轨迹模拟&虚拟位置外设神器 专业用于:钉钉,外勤365,红圈通,企业微信和…...

    2022/11/19 21:17:05
  37. 配置失败还原请勿关闭计算机怎么办,win7系统出现“配置windows update失败 还原更改 请勿关闭计算机”,长时间没反应,无法进入系统的解决方案...

    前几天班里有位学生电脑(windows 7系统)出问题了&#xff0c;具体表现是开机时一直停留在“配置windows update失败 还原更改 请勿关闭计算机”这个界面&#xff0c;长时间没反应&#xff0c;无法进入系统。这个问题原来帮其他同学也解决过&#xff0c;网上搜了不少资料&#x…...

    2022/11/19 21:17:04
  38. 一个电脑无法关闭计算机你应该怎么办,电脑显示“清理请勿关闭计算机”怎么办?...

    本文为你提供了3个有效解决电脑显示“清理请勿关闭计算机”问题的方法&#xff0c;并在最后教给你1种保护系统安全的好方法&#xff0c;一起来看看&#xff01;电脑出现“清理请勿关闭计算机”在Windows 7(SP1)和Windows Server 2008 R2 SP1中&#xff0c;添加了1个新功能在“磁…...

    2022/11/19 21:17:03
  39. 请勿关闭计算机还原更改要多久,电脑显示:配置windows更新失败,正在还原更改,请勿关闭计算机怎么办...

    许多用户在长期不使用电脑的时候&#xff0c;开启电脑发现电脑显示&#xff1a;配置windows更新失败&#xff0c;正在还原更改&#xff0c;请勿关闭计算机。。.这要怎么办呢&#xff1f;下面小编就带着大家一起看看吧&#xff01;如果能够正常进入系统&#xff0c;建议您暂时移…...

    2022/11/19 21:17:02
  40. 还原更改请勿关闭计算机 要多久,配置windows update失败 还原更改 请勿关闭计算机,电脑开机后一直显示以...

    配置windows update失败 还原更改 请勿关闭计算机&#xff0c;电脑开机后一直显示以以下文字资料是由(历史新知网www.lishixinzhi.com)小编为大家搜集整理后发布的内容&#xff0c;让我们赶快一起来看一下吧&#xff01;配置windows update失败 还原更改 请勿关闭计算机&#x…...

    2022/11/19 21:17:01
  41. 电脑配置中请勿关闭计算机怎么办,准备配置windows请勿关闭计算机一直显示怎么办【图解】...

    不知道大家有没有遇到过这样的一个问题&#xff0c;就是我们的win7系统在关机的时候&#xff0c;总是喜欢显示“准备配置windows&#xff0c;请勿关机”这样的一个页面&#xff0c;没有什么大碍&#xff0c;但是如果一直等着的话就要两个小时甚至更久都关不了机&#xff0c;非常…...

    2022/11/19 21:17:00
  42. 正在准备配置请勿关闭计算机,正在准备配置windows请勿关闭计算机时间长了解决教程...

    当电脑出现正在准备配置windows请勿关闭计算机时&#xff0c;一般是您正对windows进行升级&#xff0c;但是这个要是长时间没有反应&#xff0c;我们不能再傻等下去了。可能是电脑出了别的问题了&#xff0c;来看看教程的说法。正在准备配置windows请勿关闭计算机时间长了方法一…...

    2022/11/19 21:16:59
  43. 配置失败还原请勿关闭计算机,配置Windows Update失败,还原更改请勿关闭计算机...

    我们使用电脑的过程中有时会遇到这种情况&#xff0c;当我们打开电脑之后&#xff0c;发现一直停留在一个界面&#xff1a;“配置Windows Update失败&#xff0c;还原更改请勿关闭计算机”&#xff0c;等了许久还是无法进入系统。如果我们遇到此类问题应该如何解决呢&#xff0…...

    2022/11/19 21:16:58
  44. 如何在iPhone上关闭“请勿打扰”

    Apple’s “Do Not Disturb While Driving” is a potentially lifesaving iPhone feature, but it doesn’t always turn on automatically at the appropriate time. For example, you might be a passenger in a moving car, but your iPhone may think you’re the one dri…...

    2022/11/19 21:16:57