From 92fed0a51465d62a918a5af3884c1c8b834cb784 Mon Sep 17 00:00:00 2001 From: pethkqnpj Date: Wed, 13 Oct 2021 10:22:41 +0800 Subject: [PATCH] ADD file via upload --- EatWhatActivity.java | 132 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 EatWhatActivity.java diff --git a/EatWhatActivity.java b/EatWhatActivity.java new file mode 100644 index 0000000..9374f56 --- /dev/null +++ b/EatWhatActivity.java @@ -0,0 +1,132 @@ +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) {} //当数据库的版本号增加时调用 + } +} \ No newline at end of file