SQLite数据库

这次的作业是完成以下任务:
1)将学生信息存入数据库;
2)显示所有学生信息列表;
3)删除数据库表中第一条信息。
布局代码xml如下:

<EditText
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

<Button
    android:id="@+id/add"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="1"
    android:text="增加新纪录"
    android:textSize="18dp"/>
<Button
    android:id="@+id/delete"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="2"
    android:text="删除第一条记录"
    android:textSize="18dp"/>
</LinearLayout>
<ListView
    android:id="@+id/list_view"
    android:layout_width="match_parent"
    android:choiceMode="singleChoice"
    android:listSelector="#ffaa00"
    android:layout_weight="1"
    android:layout_height="0dp" />
</LinearLayout>

获取数据,封装相关信息:

public class Student implements Serializable {
private int _id;
private String name;
public Student(){
}
public Student(String name){
this.name=name;
}

public int get_id() {
return _id;
}

public void set_id(int _id) {
this._id = _id;
}

 public String getName() {
return name;
}

 public void setName(String name) {
 this.name = name;
 }
}

打开或创建数据库:
studentdao

public class StudentDAO {
private Context context;
private MyDBHelper helper;
private SQLiteDatabase db;

public StudentDAO(Context context) {
helper = new MyDBHelper(context);
 }

public void insert(Student student){
db = helper.getWritableDatabase();

// 第一种方法
// 2. 生成数据集合
ContentValues values = new ContentValues();
values.put("name", student.getName());
// 3. 执行语句
db.insert("student", null, values);
}


public Cursor selectAll() {
db = helper.getReadableDatabase();
Cursor cursor = db.query("student",null,null,null,null,null,null);
return cursor;
}
public void delete(int id){
db = helper.getWritableDatabase();
//根据数据的id号来删除数据
db.delete("student","_id=?",new String[]{String.valueOf(id)});
  }
 }

mydhbhelper

 public class MyDBHelper extends SQLiteOpenHelper {
//student表创建语句,字段包括:关键字id,姓名name,班级classmte,年龄age
private String sql="create table student(_id integer primary key autoincrement,"
    +"name text not null ,classmate text not null,age integer)";

public MyDBHelper(Context context) {
super(context,"studentInfo",null,1);
}
  //当app中没有数据库或数据库的版本为1的时候会被调用,且调用一次

@Override
 public void onCreate(SQLiteDatabase db) {
db.execSQL(sql);
    }
   //升级数据库表,当newVersion>oldVersion,会被调用
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table if exists stdent");
onCreate(db);
     }
  }

MyCursorAdapter

    public class MyCursorAdapter extends CursorAdapter {
    psuper(context, cursor,0);
   }

   @Override
  public View newView(Context context, Cursor cursor, ViewGroup viewGroup) {
  return LayoutInflater.from(context).inflate(R.layout.activity_main,viewGroup,false);
    }
//给item的每个主键赋值
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView name=(TextView)view.findViewById(R.id.et_name);

name.setText(cursor.getString(cursor.getColumnIndex("name")));

     }
    }

MainActivity中实现数据库表中第一条信息的删除:
MainActivity

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private ListView studentList;
private Button btnzj;
private Button btnsc;
private EditText etname;
private MyCursorAdapter adapter;
private StudentDAO studentDao;
private Student student;
private Cursor cursor;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

studentList = (ListView) findViewById(R.id.list_view);
etname=(EditText)findViewById(R.id.et_name);
btnzj = (Button) findViewById(R.id.btn_zj);
btnzj.setOnClickListener(this);
btnsc = (Button) findViewById(R.id.btn_sc);
btnsc.setOnClickListener(this);

// 创建Adapter:CursorAdpater
studentDao = new StudentDAO(this);
if(cursor != null) {
    adapter = new MyCursorAdapter(this, cursor);
    studentList.setAdapter(adapter);
}


// ListView的item点击事件
    studentList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Cursor cursor = (Cursor) adapterView.getItemAtPosition(i);
            if(cursor != null) {
                cursor.moveToPosition(i);
                student = new Student();
                student.setName(cursor.getString(cursor.getColumnIndex("name")));
            }
        }
    });
}

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_zj:
                String name = etname.getText().toString();
                //对必填项做非空判断
                if (TextUtils.isEmpty(name)) {
                    Toast.makeText(MainActivity.this, "姓名不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                //将这些数据存储到数据库
                //组装name对象
                Student student = new Student(name);
                //调用nameDAO的insert()方法插入数据库
                StudentDAO studentDAO = new StudentDAO(this);
                studentDAO.insert(student);
                Cursor cursor = studentDao.selectAll();
                adapter.changeCursor(cursor);
                Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_sc:
                if (student!= null) {
                    cursor.moveToFirst();
                    student = new Student();
                    student.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
                    studentDao.delete(student.get_id());
                    studentDao = new StudentDAO(this);
                    cursor = studentDao.selectAll();
                    adapter = new MyCursorAdapter(this, cursor);
                    studentList.setAdapter(adapter);
                    Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
                }
                    Toast.makeText(MainActivity.this, "记录为空", Toast.LENGTH_SHORT).show();
                break;
        }

        }
   }
posted @ 2017-05-16 16:25  Doranmi  阅读(192)  评论(0编辑  收藏  举报