1.数据库
public class DBopenHelper extends SQLiteOpenHelper {
private static final String DATA_NAME = "date.db";
private static final int VERSION = 1;
public DBopenHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
super(context, DATA_NAME, factory, VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "create table userTable(_id integer primary key autoincrement,"
+ "username varchar(10),userpawd varchar(20),userreay varchar(10))";
String sqls = "create table foodTable(_id integer primary key autoincrement,"
+ "foods varchar(10),howdos varchar(20))";
db.execSQL(sql);
db.execSQL(sqls);
}
2.登录界面
1.登录
在首界面点击登录命令时,判断用户名称和密码是否为空。
若有一个为空,则提示用户账号和密码不能为空;
若不为空,则获取用户输入的用户名和密码;
若无此用户名或密码错误,则清空输入框并提示;
若账号和密码正确,则跳转到单词操作界面,并提示登录成功。
#
2.注册
在首界面点击注册按钮时,从首界面进入用户注册界面,开始填写用户信息。
在用户注册界面点击注册按钮时,判断用户名称,密码,实名是否为空。若有一个为空,则提示请完善用户信息,若不为空,则获取用户输入的用户名和密码,并提示注册成功,跳转到首页面登录。
若用户名存在,则提示用户名已存在,请重新输入。
3.主页面
1.关联ListView与数据库
关联的关键就是ListView的适配器SimpleCursorAdapter
2.刷新ListView
当我们对数据进行增删改的时候,就需要将ListView刷新显示。
自定义一个方法refreshListview(),里面关键的方法是适配器对象sim的changeCursor(Cursor cursor)方法,因为需要传递一个参数cursor,所以我们需要得到这个Cursor对象,通过数据库对象的查询方法query就能返回一个Cursor对象
3.更新操作
改就是移动游标到需要更改数据的位置,获取该位置的参数,然后用ContentValues 存新的数据,最后用update方法进行数据替换更改。实现单击ListView的某一项,然后弹出一个AlertDialog进行更改。
4.删除操作
实现删除ListView的一条数据并将数据库对应的数据也删除。这里我们实现长按ListView的某一项item,然后弹出一个对话框,确认是否删除,确定的话就删除该数据,取消的话就不做任何处理。要想删除数据库的某一条数据,我们就得先知道是ListView的哪一项,所以我们先写ListView的长按事件监听,并且添加一个AlertDialog
5.查询:
设置查询button;通过文本框输入所需要查询的菜品名;遍历数据库中食物表中信息,若未匹配到文本框中输入的菜品名,则弹出对话框:无此菜品,否则返回查询到的菜品信息进行展示
initEvent();//适配器
//添加点击事件
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, final int position, long l) {//view获得操作控件 position位置 == 1第几行
AlertDialog builder1 =new AlertDialog.Builder(login.this).create();
final View mView = View.inflate(login.this, R.layout.dialog_et_layout, null); //根据布局id把这个布局加载成一个View并返回的。
mEt_foods = (EditText) mView.findViewById(R.id.et_dialog_foods); //要用对应布局的view对象去findViewById获取控件对象
mEt_howdos = (EditText) mView.findViewById(R.id.et_dialog_howdos); //将放置了两个EditText的布局渲染成view对象
mEt_foods.setText(((EditText) view.findViewById(R.id.textfood1)).getText().toString()); //获取并显示原来的内容
mEt_howdos.setText(((EditText) view.findViewById(R.id.texthowdo1)).getText().toString());
builder1.setView(mView);
builder1.setTitle("提示");
builder1.setMessage("是否修改数据");
builder1.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
updateData(position);
}
});
builder1.setButton(DialogInterface.BUTTON_NEGATIVE,"取消", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder1.show();
}
});
//长按删除
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, final View view, final int position, long l) {
new AlertDialog.Builder(login.this)
.setTitle("提示")
.setMessage("是否删除该项")
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
deleteData(position);
}
})
.setNegativeButton("取消", null)
.show();
return true;
}
});
}
private void initEvent() {
//适配器
DBO = new DBopenHelper(login.this,"date.db",null,1);
db = DBO.getWritableDatabase();
//生成SimpleAdapter适配器对象
sim=new SimpleCursorAdapter(login.this,R.layout.showfood,null,
new String[]{"foods","howdos"},
new int[]{R.id.textfood1,R.id.texthowdo1});
lv.setAdapter(sim);
refreshListview();
}
public void refreshListview() {
mcursor = db.query("foodTable",null,null,null,null,null,null);
sim.changeCursor(mcursor);
}
//查询
public void gocx(){
String keys=chaxun.getText().toString();
boolean cx = true;
mcursor = db.query("foodTable",null,null,null,null,null,null);
while (mcursor.moveToNext()){
if (keys.equals(mcursor.getString(mcursor.getColumnIndex("foods")))){
Intent intent4 = new Intent(login.this, query.class);
Bundle bundle = new Bundle();
bundle.putString("sdata",keys);
intent4.putExtras(bundle);//键名、键对应的值
startActivity(intent4);
cx=false;
break;
}
}
if(cx){
chaxun.setText("");
Toast.makeText(login.this,"暂无此菜",Toast.LENGTH_LONG).show();
}
}
}
4.添加界面
添加:
设置添加button和添加界面;通过文本框输入菜名和做法进行添加,单击添加button,首先遍历数据库中食物表部分,判定是否已有文本框中所需添加的菜品名,若有弹出对话框:已有当前菜品;若无:将需添加的菜品名和菜品信息添加至数据库中;弹出对话框,选择返回主页或继续添加;
private void oktj(){
String acm = ACM.getText().toString();
String azf = AZF.getText().toString();
boolean createFood = true;
DBO = new DBopenHelper(addfoods.this,"date.db",null,1);
db = DBO.getWritableDatabase();
if (acm.equals("")||azf.equals("")){
Toast.makeText(addfoods.this,"请完整填写内容",Toast.LENGTH_LONG).show();
}else {
mcursor = db.query("foodTable",new String[]{"foods"},null,null,null,null,null);
while (mcursor.moveToNext()){
if(acm.equals(mcursor.getString(mcursor.getColumnIndex("foods")))){
Toast.makeText(addfoods.this,"该菜品已存在",Toast.LENGTH_LONG).show();
createFood = false;
break;
}
}
if (createFood){
ContentValues values = new ContentValues();
values.put("foods",acm);
values.put("howdos",azf);
db.insert("foodTable",null,values);
db.close();
AlertDialog builder = new AlertDialog.Builder(addfoods.this).create();
builder.setTitle("添加成功");
builder.setMessage("是否返回?");
builder.setButton(DialogInterface.BUTTON_POSITIVE, "确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent6 = new Intent(addfoods.this, login.class);
startActivity(intent6);
}
});
builder.setButton(DialogInterface.BUTTON_NEGATIVE, "取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
}
}
}
}