<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E6E6E6"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="名称:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="价格:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_price"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:background="@android:color/white"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="数量:"
android:textColor="#000"
android:textSize="20sp" />
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:background="@null"
android:padding="10dp"
android:maxLines="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="添加"/>
<Button
android:id="@+id/query"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="查询"/>
<Button
android:id="@+id/update"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="修改"/>
<Button
android:id="@+id/delete"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除"/>
</LinearLayout>
<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textSize="15sp"/>
<TextView
android:id="@+id/tv_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="3dp"
android:textSize="15sp"/>
</LinearLayout>
package com.example.myapplication;
import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends Activity implements View.OnClickListener {
private EditText et_name,et_price,et_number;
private ListView listView;
private String name,price,number;
private MyHelper myHelper;
private SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et_name = findViewById(R.id.et_name);
et_price = findViewById(R.id.et_price);
et_number = findViewById(R.id.et_number);
listView = findViewById(R.id.listView);
Button add = findViewById(R.id.add);
Button query = findViewById(R.id.query);
Button update = findViewById(R.id.update);
Button delete = findViewById(R.id.delete);
add.setOnClickListener(this);
query.setOnClickListener(this);
update.setOnClickListener(this);
delete.setOnClickListener(this);
myHelper = new MyHelper(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.add:
db = myHelper.getWritableDatabase();
name = et_name.getText().toString();
price = et_price.getText().toString();
number = et_number.getText().toString();
ContentValues values = new ContentValues();
values.put("name", name);
values.put("price", price);
values.put("number", number);
db.insert("cart", null, values);
db.close();
Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show();
break;
case R.id.query:
Log.e("yanwenhua","123");
List<CartBean> list = new ArrayList<>();
db = myHelper.getWritableDatabase();
Cursor cursor = db.query("cart", null, null, null, null,
null, null);
if (cursor.getCount() == 0) {
Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show();
} else {
while (cursor.moveToNext()) {
CartBean cartBean = new CartBean();
int nameIndex = cursor.getColumnIndex("name");
int priceIndex = cursor.getColumnIndex("price");
int numberIndex = cursor.getColumnIndex("number");
String name = cursor.getString(nameIndex);
String price = cursor.getString(priceIndex);
String number = cursor.getString(numberIndex);
Log.e("yanwenhua","cursor.getCount();--"+cursor.getCount()+"name-"+name+" "+price+" "+number);
cartBean.setName(name);
cartBean.setPrice(price);
cartBean.setNumber(number);
list.add(cartBean);
}
CartAdapter adapter = new CartAdapter(MainActivity.this,list);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
cursor.close();
db.close();
break;
case R.id.update:
name = et_name.getText().toString();
price = et_price.getText().toString();
number = et_number.getText().toString();
db = myHelper.getWritableDatabase();
values = new ContentValues();
values.put("number",number);
values.put("price",price);
db.update("cart", values, "name=?",
new String[]{name});
db.close();
Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show();
break;
case R.id.delete:
db = myHelper.getWritableDatabase();
db.delete("cart", null, null);
List<CartBean> list2 = new ArrayList<>();
CartAdapter adapter = new CartAdapter(MainActivity.this,list2);
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
db.close();
Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show();
break;
}
}
}
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "shoppingcart.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE cart(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), price VARCHAR(20), number VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "shoppingcart.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE cart(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), price VARCHAR(20), number VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
package com.example.myapplication;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context) {
super(context, "shoppingcart.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE cart(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), price VARCHAR(20), number VARCHAR(20))");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}