安卓开发:学生信息管理小程序
程序员文章站
2022-04-13 11:44:17
...
成品图
打开程序,实现添加学生,及时显示数据库内容,男女分别用相应的图片表示,点击垃圾桶可以删除这一行的学生,刷新数据,停留在刚才的位置:
1.activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="学生信息管理系统" />
<EditText
android1:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android1:hint="请输入学生姓名" />
<RadioGroup
android1:orientation="horizontal"
android1:id="@+id/sex"
android1:layout_width="match_parent"
android1:layout_height="wrap_content"
>
<RadioButton
android1:id="@+id/male"
android1:layout_width="0dp"
android1:layout_weight="1"
android1:layout_height="wrap_content"
android1:checked="true"
android1:text="男" />
<RadioButton
android1:id="@+id/female"
android1:layout_weight="1"
android1:layout_width="0dp"
android1:layout_height="wrap_content"
android1:text="女" />
</RadioGroup>
<Button
android1:id="@+id/saveInfo"
android:onClick="saveInfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android1:text="保存"
/>
<!-- 能够处理上万条滚动数据的滑动
原理是能够及时回收没有在屏幕显示的对象 -->
<ListView
android1:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="match_parent"
></ListView>
</LinearLayout>
2. item.xml(显示数据库学生的信息)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<ImageView
android:id="@+id/iv_sex"
android:layout_width="25dp"
android:layout_height="25dp"
<!-- 需要在drawable-hdpi 中添加男人标志图片 -->
android:src="@drawable/man"
/>
<TextView
android:id="@+id/tv_name"
android:layout_toRightOf="@id/iv_sex"
android:gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="姓名"
android:textSize="22sp"
android:textColor="#ff0000"
android:layout_marginLeft="20dp"
/>
<ImageView
android:id="@+id/iv_delete"
android:layout_alignParentRight="true"
android:layout_marginRight="50dp"
android:layout_width="25dp"
android:layout_height="25dp"
<!-- 需要在drawable-hdpi 中添加垃圾桶图片 -->
android:src="@drawable/delete"
/>
</RelativeLayout>
3. MainActivity.java
package com.zt.studentsystemv2;
import java.util.List;
import com.zt.studentdbhelper.Student;
import com.zt.studentdbhelper.StudentDao;
import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private EditText name;
private RadioButton male,female;
private ListView lv;
private Button save;
// private LinearLayout viewInfo;
private String stuName,sex;
StudentDao dao = new StudentDao(this, "student.db", null, 1);
boolean isStart=true;
private List<Student> stuInfo;
private MyAdapter myAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// viewInfo = (LinearLayout) findViewById(R.id.info);
lv=(ListView) findViewById(R.id.lv);
refreshData();
}
private void init() {
name = (EditText) findViewById(R.id.name);
male = (RadioButton) findViewById(R.id.male);
female = (RadioButton) findViewById(R.id.female);
save = (Button) findViewById(R.id.saveInfo);
stuName = name.getText().toString().trim();
if (TextUtils.isEmpty(stuName)){
alter("姓名不能为空");
isStart=false;
return ;
}
sex = male.isChecked()?"男":"女";
isStart=true;
}
private void alter(String info) {
Toast.makeText(this, info, 5).show();
}
public void saveInfo(View v) {
init();
if (!isStart) return;
dao.add(stuName, sex);
alter("保存成功!");
refreshData();
}
private void refreshData() {
stuInfo = dao.find();
if (myAdapter==null){
myAdapter = new MyAdapter();
lv.setAdapter(myAdapter);
}else{
//通知数据适配器更新数据,而不是new出来新的数据适配器
myAdapter.notifyDataSetChanged();
}
}
class MyAdapter extends BaseAdapter{
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View item = null;
if (convertView==null){
//把一个布局xml文件转化成view对象
item = View.inflate(MainActivity.this,R.layout.item, null);
}else{
//利用回收的对象
item = convertView;
}
ImageView sex=(ImageView) item.findViewById(R.id.iv_sex);
Student stu = stuInfo.get(position);
if ("男".equals(stu.getSex())){
sex.setImageResource(R.drawable.man);
}else{
sex.setImageResource(R.drawable.woman);
}
TextView tv = (TextView) item.findViewById(R.id.tv_name);
tv.setText(stu.getName());
ImageView delete = (ImageView) item.findViewById(R.id.iv_delete);
delete.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Student stu = stuInfo.get(position);
//从数据库删除数据
dao.delete(stu.getName(), stu.getSex());
alter("删除成功!");
//更新ui
refreshData();
}
});
return item;
}
@Override
public int getCount() {
return stuInfo.size();
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
}
}
4.sqlLite数据库的操作:
Student.java:
package com.zt.studentdbhelper;
public class Student {
private String name;
private String sex;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
StudentDBHelper.java:
package com.zt.studentdbhelper;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class StudentDBHelper extends SQLiteOpenHelper {
public StudentDBHelper(Context context, String name, CursorFactory factory,
int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table student(_id integer primary key autoincrement,name varchar(20),sex varchar(4) check(sex in ('男','女')))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.i("MainActivity","建表完成!");
}
}
StudentDao:
package com.zt.studentdbhelper;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
public class StudentDao {
private StudentDBHelper helper;
public StudentDao(Context context, String name, CursorFactory factory,
int version) {
helper=new StudentDBHelper(context, name, factory, version);
}
/**
* 添加一个学生
* @param name
* @param phone
*/
public void add(String name,String sex) {
SQLiteDatabase sqlHelper = helper.getWritableDatabase();
sqlHelper.execSQL("insert into student(name,sex)values(?,?)", new Object[]{name,sex});
sqlHelper.close();
}
public void delete(String name,String sex) {
SQLiteDatabase sqlHelper = helper.getWritableDatabase();
sqlHelper.execSQL("delete from student where name =? and sex=?", new Object[]{name,sex});
sqlHelper.close();
}
public List<Student> find() {
List<Student> list = new ArrayList<Student>();
SQLiteDatabase sqlHelper = helper.getReadableDatabase();
Cursor cur = sqlHelper.rawQuery("select name,sex from student where name is not null and sex is not null", null);
boolean result = cur.moveToNext();
while (result){
Student stu= new Student();
stu.setName(cur.getString(0));
stu.setSex(cur.getString(1));
list.add(stu);
result= cur.moveToNext();
}
cur.close();
sqlHelper.close();
return list;
}
}
查看数据库:
将/sdk/platform-tools加入环境变量;
cmd下使用adb shell ,cd 进入相应包,进入数据库目录
输入sqLite3 student.db,select * from student;
推荐阅读
-
小程序scroll-view安卓机隐藏横向滚动条的实现详解
-
如何使用猎码安卓开发工具?使用猎码安卓开发工具编译猎码程序教程
-
微信小程序 wx.uploadFile在安卓手机上面the same task is working问题解决
-
小程序scroll-view安卓机隐藏横向滚动条的实现详解
-
QQ正式上线小程序 安卓用户先行体验
-
微信安卓新版本7.0.3内测:首页小程序变全屏
-
微信小程序 video 安卓端可以播放 ios 端播放失败
-
安卓微信7.0.3正式版发布:首页小程序全屏
-
小程序 ----踩坑 ---安卓iOS兼容等
-
小白开发安卓程序之路(2-2)TextView(解决 点击button不跳转、跑马灯跑不起来、Android studio 启动时黑屏)