Activity、:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:gravity="center"
android:text="商店"
android:textSize="55dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<TextView
style="@style/wq_1"
android:gravity="center"
android:text="名称:"></TextView>
<TextView
style="@style/wq_1"
android:gravity="center"
android:text="价格:"></TextView>
<Button
android:id="@+id/add"
style="@style/wq_1"
android:layout_marginRight="2dp"
android:background="#125FE4"
android:text="添加"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<EditText
android:id="@+id/yname"
style="@style/wq_1"
android:gravity="center"
android:hint="请输入名称" />
<EditText
android:id="@+id/sprice"
style="@style/wq_1"
android:gravity="center"
android:hint="请输入价格" />
<Button
android:id="@+id/query"
style="@style/wq_1"
android:layout_marginRight="2dp"
android:background="#A68159"
android:text="查询"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<TextView
style="@style/wq_1"
android:gravity="center"
android:text="大小:"></TextView>
<TextView
style="@style/wq_1"
android:gravity="center"
android:text="数量:"></TextView>
<Button
android:id="@+id/update"
style="@style/wq_1"
android:layout_marginRight="2dp"
android:background="#DD7D07"
android:text="修改"></Button>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:orientation="horizontal">
<EditText
android:id="@+id/ysize"
style="@style/wq_1"
android:gravity="center"
android:hint="请输入大小" />
<EditText
android:id="@+id/ynum"
style="@style/wq_1"
android:gravity="center"
android:hint="请输入数量"/>
<Button
android:id="@+id/delete"
style="@style/wq_1"
android:layout_marginRight="2dp"
android:background="#731EE9"
android:text="删除"></Button>
</LinearLayout>
<ListView
android:id="@+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
MainActivity:
package com.example.yangtaozhou;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText yname;
private EditText sprice;
private EditText ysize;
private EditText ynum;
private Button add;
private Button query;
private Button update;
private Button delete;
private SQLiteDatabase db;
private ListView lv;
private SimpleCursorAdapter adapter;
String form[] =new String[]{"yname","sprice","ysize","ynum"};
int to[] =new int[]{R.id.tv1,R.id.tv2,R.id.tv3,R.id.tv4};
private MyHelper myHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myHelper = new MyHelper(this);
initView();
}
private void initView() {
yname = findViewById(R.id.yname);
sprice = findViewById(R.id.sprice);
ysize = findViewById(R.id.ysize);
ynum = findViewById(R.id.ynum);
add = findViewById(R.id.add);
query = findViewById(R.id.query);
update = findViewById(R.id.update);
delete = findViewById(R.id.delete);
add.setOnClickListener(this);
query.setOnClickListener(this);
update.setOnClickListener(this);
delete.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
addyy();
break;
case R.id.query:
queryyy();
break;
case R.id.update:
updateyy();
break;
case R.id.delete:
deleteyy();
break;
}
}
private void deleteyy() {
db = myHelper.getWritableDatabase();
db.delete("yy","name=?",new String[]{yname.getText().toString()});
Toast.makeText(this,"信息已删除",Toast.LENGTH_SHORT).show();
db.close();
queryyy();
}
private void updateyy() {
db = myHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("sprice",sprice.getText().toString());
values.put("ssize",ysize.getText().toString());
values.put("snum",ynum.getText().toString());
db.update("yy",values,"name=?",new String[]{yname.getText().toString()});
Toast.makeText(this,"信息已更改",Toast.LENGTH_SHORT).show();
db.close();
queryyy();
}
private void queryyy() {
lv = findViewById(R.id.lv);
db = myHelper.getReadableDatabase();
Cursor cursor = db.query("yy", null, null, null, null, null, null);
adapter = new SimpleCursorAdapter(this, R.layout.list_item, cursor, form, to, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
lv.setAdapter(adapter);
db.close();
}
private void addyy() {
db = myHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("yname",yname.getText().toString());
values.put("sprice",sprice.getText().toString());
values.put("ysize",ysize.getText().toString());
values.put("ynum",ynum.getText().toString());
db.insert("yy",null,values);
Toast.makeText(this,"信息已添加",Toast.LENGTH_SHORT).show();
db.close();
queryyy();
}
}
MyHelper:
package com.example.yangtaozhou;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(@Nullable Context context) {
super(context, "ytz", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table yy(_id integer primary key autoincrement,yname varchar(20),sprice integer,ysize varchar(20),ynum varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
list_item.xml:
package com.example.yangtaozhou;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import androidx.annotation.Nullable;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(@Nullable Context context) {
super(context, "ytz", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table yy(_id integer primary key autoincrement,yname varchar(20),sprice integer,ysize varchar(20),ynum varchar(20))");
}
@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
themes:
<style name="wq_1">
<item name="android:layout_width">2dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:textSize">30dp</item>
</style>