先看下运行结果
长按会出现一个对话框
第一步:把两个布局画好
第二步:来看下代码部分(创建表、数据库)
//创建数据库,创建表
public class DBHelper extends SQLiteOpenHelper{
private static final String DB="course.db";//数据库名
private static final int V=1;//数据库版本
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
public DBHelper(Context context){
super(context,DB,null,V);
/**
* 第一个参数:上下文
* 第二个参数:数据库名
* 第三个参数:创建数据库的工厂方法
* 第四个参数:数据库版本
* */
}
@Override
//创建表
public void onCreate(SQLiteDatabase sqLiteDatabase) {//表名:course 字段:EditName String,teacher String,Xs String,Xf String
sqLiteDatabase.execSQL("CREATE TABLE IF NOT EXISTS course(_id INTEGER PRIMARY KEY AUTOINCREMENT, EditName String,teacher String,Xs String,Xf String)");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
第二步:实体类用于承载数据
public class CourseDataBean implements Serializable {
private String editName;
private String teacher, xs, xf;
public String getEditName() {return editName;}
public void setEditName(String editName) {
this.editName = editName;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {this.teacher = teacher;}
public String getXs() {
return xs;
}
public void setXs(String xs) {
this.xs = xs;
}
public String getXf() {
return xf;
}
public void setXf(String xf) {
this.xf = xf;
}
}
第三步:主要是对数据进行数据的增删改查操作,是基于DBelper类来操作。
public class DBManager {
private SQLiteDatabase db;
private DBHelper mDBHelper;
private CourseDataBean course;
public DBManager(Context context) {
mDBHelper = new DBHelper(context);//实例化类
db = mDBHelper.getWritableDatabase();//对数据进行操作
}
/**
* 添加数据向CourseDataBean类中插入数据
*/
public void addData(CourseDataBean course){
db.beginTransaction();//开启事务
try {
db.execSQL("INSERT INTO course(editName, teacher, xs, xf) VALUES(?,?,?,?)",new Object[]{course.getEditName(),course.getTeacher(),course.getXs(),course.getXf()});
db.beginTransaction();//事务成功
}finally {
db.endTransaction();//结束事务
}
}
/**
* 修改数据
* ContentValues:是一个类
*/
public void changeData(String oldCoureName ,String editName,String teacher,String xs,String xf){
ContentValues cv=new ContentValues();//实例化类
cv.put("editName",editName);//添加要更改的字段及内容
cv.put("teacher",teacher);
cv.put("xs",xs);
cv.put("xf",xf);
//执行修改
db.update("course",cv,"editName=?",new String[]{oldCoureName});
}
/**
* 通过name来删除数据
*/
public void delData(String editName){
String[] arg={editName};
db.delete("course","editName=?",arg);
}
/**
* 执行SQL命令返回list,
* ArrayList集合
*/
public ArrayList info() {
//集合 泛型类
ArrayList list = new ArrayList();
//游标 查询 全部字段在这张表中 第二个参数:查询的条件
Cursor c = db.rawQuery("select * from course", null);
while (c.moveToNext()) {
//new一个对象
course = new CourseDataBean();
/**
* 从CourseDataBean读取数据,并且把数据放到course对象中
* c.getColumnIndex("courseName"):获取到CourseDataBean的值的下标
* c.getString(c.getColumnIndex("courseName")):获取到它的值
* */
course.setEditName(c.getString(c.getColumnIndex("EditName")));
course.setTeacher(c.getString(c.getColumnIndex("teacher")));
course.setXs(c.getString(c.getColumnIndex("Xs")));
course.setXf(c.getString(c.getColumnIndex("Xf")));
list.add(course);
}
c.close();
return list;
}
public void closeDB(){db.close();}
}
第四步:也是最后一步,就是对主布局进行写
~~~public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button btn_add;
private EditText et_EditName;
private ListView listView;
private DBManager manager;
private CourseDataBean bean;
private DBHelper helper;
private EditText dialogEditName, dialogEditTeacher, dialogEditXs, dialogEditXf;
private Context context;
private List datas;//集合
private int position;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = MainActivity.this;
manager = new DBManager(context);
bean = new CourseDataBean();//实例化
init();
}
private void init() {
//主布局控件的实例化
btn_add = (Button) findViewById(R.id.add);
btn_add.setOnClickListener(this);
et_EditName = (EditText) findViewById(R.id.EditName);
listView = (ListView) findViewById(R.id.listView);
//ListView的点击事件
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> adapterView, View view, int i, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
//创建对话框,添加一个布局
View view1 = View.inflate(context, R.layout.layout, null);
//承载对话框
builder.setView(view1);
position = i;
//对话框中的控件的实例化
dialogEditName = (EditText) view1.findViewById(R.id.dialog_EditName);
dialogEditTeacher = (EditText) view1.findViewById(R.id.dialog_EditTeacher);
dialogEditXf = (EditText) view1.findViewById(R.id.dialog_EditXf);
dialogEditXs = (EditText) view1.findViewById(R.id.dialog_EditXs);
//datas.get(i).getCourseName():从这个位置找到这个值赋给控件
dialogEditName.setText(datas.get(i).getEditName());
dialogEditTeacher.setText(datas.get(i).getTeacher());
dialogEditXs.setText(datas.get(i).getXs());
dialogEditXf.setText(datas.get(i).getXf());
//取消
builder.setNeutralButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dialogInterface.dismiss();
}
});
//删除
builder.setNegativeButton("删除", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//数据库调用这个方法,
manager.delData(dialogEditName.getText().toString());
Toast.makeText(MainActivity.this, "删除成功", Toast.LENGTH_SHORT).show();
//方法的作用:是为了显示到ListView列表上
adapter1();
//对话框退出
dialogInterface.dismiss();
}
});
//保存(修改)
builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//在这个集合中找到要修改的名字赋给name
String name = datas.get(position).getEditName();
//调用这个方法
manager.changeData(name, dialogEditName.getText().toString(), dialogEditTeacher.getText().toString(), dialogEditXs.getText().toString(), dialogEditXf.getText().toString());
adapter1();
dialogInterface.dismiss();
}
});
//创建数据库并show出来
builder.create().show();
}
});
}
//添加
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.add:
CourseDataBean c = new CourseDataBean();//实例化对象
//给对话框中的EditText控件赋值
c.setEditName(et_EditName.getText().toString());
c.setXf("默认");
c.setXs("默认");
c.setTeacher("默认");
manager.addData(c);//添加到方法中
Toast.makeText(MainActivity.this, "添加成功", Toast.LENGTH_SHORT).show();
adapter1();
break;
}
}
private void adapter1() {
datas = manager.info();//取出数据库里面胡数据
//数据源//创建一个集合
List data = new ArrayList<>();
for (int i = 0; i < datas.size(); i++) {
data.add(datas.get(i).getEditName());
}
//创建一个数据库
ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_expandable_list_item_1, data);
//与ListView列表绑定并将数据加载到列表上
listView.setAdapter(adapter);
}
}
~~~