|
|
package com.example.WhatMeal;
|
|
|
|
|
|
import android.content.ContentValues;
|
|
|
import android.content.Intent;
|
|
|
import android.content.Context;
|
|
|
import android.database.Cursor;
|
|
|
import android.database.sqlite.SQLiteDatabase;
|
|
|
import android.database.sqlite.SQLiteOpenHelper;
|
|
|
import androidx.appcompat.app.AppCompatActivity;
|
|
|
import android.os.Bundle;
|
|
|
import android.view.View;
|
|
|
import android.widget.Button;
|
|
|
import android.widget.EditText;
|
|
|
import android.widget.TextView;
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
public class MenuActivity extends AppCompatActivity implements View.OnClickListener
|
|
|
{
|
|
|
MyHelper myHelper;
|
|
|
private Button back;
|
|
|
private EditText mEtName;
|
|
|
private EditText mEtStore;
|
|
|
private TextView mShow;
|
|
|
private Button mBtnAdd;
|
|
|
private Button mBtnQuery;
|
|
|
private Button mBtnUpdate;
|
|
|
private Button mBtnDelete;
|
|
|
@Override
|
|
|
protected void onCreate(Bundle savedInstanceState)
|
|
|
{
|
|
|
super.onCreate(savedInstanceState);
|
|
|
setContentView(R.layout.activity_menu);
|
|
|
myHelper = new com.example.WhatMeal.MenuActivity.MyHelper(this);
|
|
|
back = (Button) findViewById(R.id.back);
|
|
|
back.setOnClickListener(new View.OnClickListener()
|
|
|
{
|
|
|
@Override
|
|
|
public void onClick(View v)
|
|
|
{
|
|
|
Intent intent = new Intent(MenuActivity.this, MainActivity.class);
|
|
|
startActivity(intent);
|
|
|
}
|
|
|
});
|
|
|
init();//初始化控件
|
|
|
}
|
|
|
private void init()
|
|
|
{
|
|
|
mEtName = (EditText) findViewById(R.id.et_name);
|
|
|
mEtStore = (EditText) findViewById(R.id.et_store);
|
|
|
mShow = (TextView) findViewById(R.id.show);
|
|
|
mBtnAdd = (Button) findViewById(R.id.btn_add);
|
|
|
mBtnQuery = (Button) findViewById(R.id.btn_query);
|
|
|
mBtnUpdate = (Button) findViewById(R.id.btn_update);
|
|
|
mBtnDelete = (Button) findViewById(R.id.btn_delete);
|
|
|
mBtnAdd.setOnClickListener(this);
|
|
|
mBtnQuery.setOnClickListener(this);
|
|
|
mBtnUpdate.setOnClickListener(this);
|
|
|
mBtnDelete.setOnClickListener(this);
|
|
|
}
|
|
|
@Override
|
|
|
public void onClick(View v)
|
|
|
{
|
|
|
String name;
|
|
|
String store;
|
|
|
SQLiteDatabase db;
|
|
|
ContentValues values;
|
|
|
switch (v.getId())
|
|
|
{
|
|
|
case R.id.btn_add: //添加数据
|
|
|
name = mEtName.getText().toString();
|
|
|
store = mEtStore.getText().toString();
|
|
|
db = myHelper.getWritableDatabase();//获取可读写SQLiteDatabase对象
|
|
|
values = new ContentValues(); //创建ContentValue对象
|
|
|
values.put("foodname", name); //将数据添加到ContentValues对象
|
|
|
values.put("foodstore", store);
|
|
|
db.insert("information", "name", values);
|
|
|
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
|
|
|
db.close();
|
|
|
break;
|
|
|
case R.id.btn_query: //查询数据
|
|
|
db = myHelper.getReadableDatabase();
|
|
|
Cursor cursor = db.query("information", null, null, null, null, null, null);
|
|
|
if(cursor.getCount() == 0)
|
|
|
{
|
|
|
mShow.setText("");
|
|
|
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
cursor.moveToFirst();
|
|
|
mShow.setText("菜名:" + cursor.getString(1) + ";店名:" + cursor.getString(2));
|
|
|
}
|
|
|
while (cursor.moveToNext())
|
|
|
{
|
|
|
mShow.append("\n" + "菜名:" + cursor.getString(1) + ";店名:" + cursor.getString(2));
|
|
|
}
|
|
|
cursor.close();
|
|
|
db.close();
|
|
|
break;
|
|
|
case R.id.btn_update: //修改数据
|
|
|
db = myHelper.getWritableDatabase();
|
|
|
values = new ContentValues();
|
|
|
values.put("foodstore", store = mEtStore.getText().toString());
|
|
|
db.update("information", values, "foodname=?", new String[]{mEtName.getText().toString()});
|
|
|
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
|
|
|
db.close();
|
|
|
break;
|
|
|
case R.id.btn_delete: //删除数据
|
|
|
db = myHelper.getWritableDatabase();
|
|
|
db.delete("information", null, null);
|
|
|
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
|
|
|
mShow.setText("");
|
|
|
db.close();
|
|
|
break;
|
|
|
}
|
|
|
}
|
|
|
|
|
|
static class MyHelper extends SQLiteOpenHelper
|
|
|
{
|
|
|
public MyHelper(Context context) //数据库第一次被创建时调用方法
|
|
|
{
|
|
|
super(context, "EatWhat.db", null, 2);
|
|
|
}
|
|
|
@Override
|
|
|
public void onCreate(SQLiteDatabase db) //初始化数据库的表结构,执行一条建表的SQL语句
|
|
|
{
|
|
|
db.execSQL("CREATE TABLE information(_id INTEGER PRIMARY KEY AUTOINCREMENT, foodname VARCHAR(40), foodstore VARCHAR(40))");
|
|
|
}
|
|
|
@Override
|
|
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {} //当数据库的版本号增加时调用
|
|
|
}
|
|
|
} |