1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="wrap_content" 6 android:orientation="vertical" 7 tools:context=".MainActivity"> 8 9 <TextView 10 android:id="@+id/textView" 11 android:layout_width="match_parent" 12 android:layout_height="wrap_content" 13 android:background="#FF5722" 14 android:gravity="center_horizontal" 15 android:text="购物车" 16 android:textSize="24sp" /> 17 18 <TableLayout 19 android:layout_width="match_parent" 20 android:layout_height="match_parent"> 21 22 <TableRow 23 android:layout_width="match_parent" 24 android:layout_height="match_parent" 25 android:gravity="center_horizontal"> 26 27 <TextView 28 android:id="@+id/textView2" 29 android:layout_width="wrap_content" 30 android:layout_height="wrap_content" 31 android:text="物品名:" /> 32 33 <EditText 34 android:id="@+id/et_1" 35 android:layout_width="wrap_content" 36 android:layout_height="wrap_content" 37 android:ems="10" 38 android:inputType="textPersonName" /> 39 </TableRow> 40 41 <TableRow 42 android:layout_width="match_parent" 43 android:layout_height="match_parent" 44 android:gravity="center_horizontal"> 45 46 <TextView 47 android:id="@+id/textView4" 48 android:layout_width="wrap_content" 49 android:layout_height="wrap_content" 50 android:text="价格:" /> 51 52 <EditText 53 android:id="@+id/et_2" 54 android:layout_width="match_parent" 55 android:layout_height="wrap_content" 56 android:ems="10" 57 android:inputType="textPersonName" /> 58 </TableRow> 59 60 <TableRow 61 android:layout_width="match_parent" 62 android:layout_height="match_parent" 63 android:gravity="center_horizontal"> 64 65 <TextView 66 android:id="@+id/textView5" 67 android:layout_width="wrap_content" 68 android:layout_height="wrap_content" 69 android:text="数量:" /> 70 71 <EditText 72 android:id="@+id/et_3" 73 android:layout_width="wrap_content" 74 android:layout_height="wrap_content" 75 android:ems="10" 76 android:inputType="textPersonName" /> 77 78 </TableRow> 79 80 </TableLayout> 81 82 <LinearLayout 83 android:layout_width="match_parent" 84 android:layout_height="match_parent" 85 android:orientation="horizontal"> 86 87 <Button 88 android:id="@+id/add" 89 android:layout_width="match_parent" 90 android:layout_height="match_parent" 91 android:layout_weight="1" 92 android:onClick="add_Click" 93 android:text="加入购物车" /> 94 95 <Button 96 android:id="@+id/delete" 97 android:layout_width="match_parent" 98 android:layout_height="match_parent" 99 android:layout_weight="1" 100 android:onClick="delete_Click" 101 android:text="从购物车中删除" /> 102 103 <Button 104 android:id="@+id/query" 105 android:layout_width="match_parent" 106 android:layout_height="match_parent" 107 android:layout_weight="1" 108 android:onClick="find_Click" 109 android:text="查看购物车" /> 110 111 <Button 112 android:id="@+id/update" 113 android:layout_width="match_parent" 114 android:layout_height="match_parent" 115 android:layout_weight="1" 116 android:onClick="update_Click" 117 android:text="修改购物车中物品" /> 118 119 </LinearLayout> 120 121 <LinearLayout 122 android:layout_width="match_parent" 123 android:layout_height="match_parent" 124 android:orientation="horizontal"> 125 126 <TextView 127 android:id="@+id/textView3" 128 android:layout_width="40dp" 129 android:layout_height="wrap_content" 130 android:layout_weight="1" 131 android:gravity="center_horizontal" 132 android:text="物品" /> 133 134 <TextView 135 android:id="@+id/textView6" 136 android:layout_width="40dp" 137 android:layout_height="wrap_content" 138 android:layout_weight="1" 139 android:gravity="center_horizontal" 140 android:text="价格" /> 141 142 <TextView 143 android:id="@+id/textView7" 144 android:layout_width="40dp" 145 android:layout_height="wrap_content" 146 android:layout_weight="1" 147 android:gravity="center_horizontal" 148 android:text="数量" /> 149 </LinearLayout> 150 151 <LinearLayout 152 android:layout_width="match_parent" 153 android:layout_height="match_parent" 154 android:orientation="vertical"> 155 156 <ListView 157 android:id="@+id/list" 158 android:layout_width="match_parent" 159 android:layout_height="match_parent" 160 android:layout_weight="1" /> 161 </LinearLayout> 162 163 </LinearLayout>
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent"> 5 6 <TextView 7 android:id="@+id/t_name" 8 android:layout_width="40dp" 9 android:layout_height="30dp" 10 android:layout_weight="1" 11 android:background="@drawable/txt_shap" 12 android:gravity="left" 13 android:textSize="18sp" 14 android:textStyle="bold" /> 15 16 <TextView 17 android:id="@+id/t_price" 18 android:layout_width="40dp" 19 android:layout_height="30dp" 20 android:layout_weight="1" 21 android:background="@drawable/txt_shap" 22 android:gravity="right" 23 android:textSize="18sp" /> 24 25 <TextView 26 android:id="@+id/t_number" 27 android:layout_width="40dp" 28 android:layout_height="30dp" 29 android:layout_weight="1" 30 android:background="@drawable/txt_shap" 31 android:gravity="left" 32 android:textSize="18sp" /> 33 34 </LinearLayout>
1 package com.example.myapplication; 2 3 public class Sqldata { 4 5 private int id; 6 private String name,price,number; 7 8 public Sqldata() { 9 } 10 11 public Sqldata(int id, String name, String price, String number) { 12 this.id = id; 13 this.name = name; 14 this.price = price; 15 this.number = number; 16 } 17 18 public Sqldata(String name, String price, String number) { 19 this.name = name; 20 this.price = price; 21 this.number = number; 22 } 23 24 public int getId() { 25 return id; 26 } 27 28 public void setId(int id) { 29 this.id = id; 30 } 31 32 public String getName() { 33 return name; 34 } 35 36 public void setName(String name) { 37 this.name = name; 38 } 39 40 public String getPrice() { 41 return price; 42 } 43 44 public void setPrice(String price) { 45 this.price = price; 46 } 47 48 public String getNumber() { 49 return number; 50 } 51 52 public void setNumber(String number) { 53 this.number = number; 54 } 55 }
1 package com.example.myapplication; 2 3 import android.content.Context; 4 import android.database.sqlite.SQLiteDatabase; 5 import android.database.sqlite.SQLiteOpenHelper; 6 7 import androidx.annotation.Nullable; 8 9 public class MyHelper extends SQLiteOpenHelper { 10 public MyHelper(@Nullable Context context) { 11 super(context, "car.db", null, 1); 12 } 13 14 @Override 15 public void onCreate(SQLiteDatabase db) { 16 db.execSQL("create table car(_id integer primary key autoincrement,name varchar(50),price varchar(50),number varchar(50))"); 17 } 18 19 @Override 20 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 21 22 } 23 }
1 package com.example.myapplication; 2 3 import androidx.annotation.NonNull; 4 import androidx.appcompat.app.AppCompatActivity; 5 6 import android.content.ContentValues; 7 import android.content.Context; 8 import android.content.Intent; 9 import android.database.Cursor; 10 import android.database.sqlite.SQLiteDatabase; 11 import android.os.Bundle; 12 import android.view.LayoutInflater; 13 import android.view.View; 14 import android.view.ViewGroup; 15 import android.widget.ArrayAdapter; 16 import android.widget.BaseAdapter; 17 import android.widget.Button; 18 import android.widget.EditText; 19 import android.widget.ListView; 20 import android.widget.TextView; 21 import android.widget.Toast; 22 23 import java.util.ArrayList; 24 import java.util.List; 25 26 public class MainActivity extends AppCompatActivity implements View.OnClickListener { 27 28 Intent intent = new Intent(); 29 EditText name,price,number; 30 Button add; 31 ListView lv; 32 private MyHelper helper; 33 List<Sqldata> list = new ArrayList<>(); 34 @Override 35 protected void onCreate(Bundle savedInstanceState) { 36 super.onCreate(savedInstanceState); 37 setContentView(R.layout.activity_main); 38 39 name = (EditText)findViewById(R.id.et_1); 40 price = (EditText)findViewById(R.id.et_2); 41 number = (EditText)findViewById(R.id.et_3); 42 43 add = (Button)findViewById(R.id.add); 44 45 lv = (ListView)findViewById(R.id.list); 46 47 helper = new MyHelper(this); 48 49 50 51 52 } 53 54 55 public void add_Click(View view){ 56 Sqldata date = new Sqldata(name.getText().toString(),price.getText().toString(),number.getText().toString()); 57 insert(date); 58 Toast.makeText(MainActivity.this,name.getText().toString()+"已添加到购物车",Toast.LENGTH_SHORT).show(); 59 } 60 61 public void delete_Click(View view){ 62 int num = delete(name.getText().toString()); 63 if (num > 0){ 64 Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show(); 65 }else{ 66 Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); 67 } 68 } 69 70 71 72 73 public void update_Click(View view){ 74 update(name.getText().toString(),price.getText().toString(),number.getText().toString()); 75 } 76 77 78 79 public void find_Click(View view){ 80 find(name.getText().toString()); 81 82 } 83 84 85 86 @Override 87 public void onClick(View v) { 88 89 } 90 91 92 93 94 //添加到购物车 95 96 public long insert(Sqldata sqldata){ 97 long id = 0; 98 SQLiteDatabase db =null; 99 100 try { 101 db = helper.getWritableDatabase(); 102 103 ContentValues values = new ContentValues(); 104 105 values.put("name",sqldata.getName()); 106 values.put("price",sqldata.getPrice()); 107 values.put("number",sqldata.getNumber()); 108 109 db.insert("car",null,values); 110 } catch (Exception e) { 111 e.printStackTrace(); 112 } finally { 113 if (db != null){ 114 db.close(); 115 } 116 } 117 118 119 return id; 120 } 121 122 //从购物车中删除 123 124 public int delete(String name){ 125 126 SQLiteDatabase db = null; 127 int num = 0; 128 try { 129 db = helper.getWritableDatabase(); 130 num = db.delete("car","name = ?",new String[]{name}); 131 } catch (Exception e) { 132 e.printStackTrace(); 133 } finally { 134 if (db!=null){ 135 db.close(); 136 } 137 } 138 return num; 139 } 140 141 //查看购物车 142 143 public List<Sqldata> find(String name){ 144 SQLiteDatabase db = null; 145 db = helper.getReadableDatabase(); 146 MyBaseAdapter myBaseAdapter = new MyBaseAdapter(this,R.layout.item,list); 147 try { 148 149 Cursor cursor = db.query("car",null,null,null,null,null,null); 150 if (cursor.getCount()!=0){ 151 while (cursor.moveToNext()){ 152 Sqldata find = new Sqldata(); 153 find.setName(cursor.getString(cursor.getColumnIndex("name"))); 154 find.setPrice(cursor.getString(cursor.getColumnIndex("price"))); 155 find.setNumber(cursor.getString(cursor.getColumnIndex("number"))); 156 list.add(find); 157 lv.setAdapter(myBaseAdapter); 158 } 159 } 160 cursor.close(); 161 162 } catch (Exception e) { 163 e.printStackTrace(); 164 } finally { 165 if (db != null){ 166 db.close(); 167 } 168 } 169 170 return list; 171 172 } 173 174 175 //对购物车进行修改 176 177 public int update(String name,String price,String number){ 178 SQLiteDatabase db = null; 179 int num = 0; 180 try { 181 db = helper.getWritableDatabase(); 182 ContentValues values = new ContentValues(); 183 values.put("price",price); 184 values.put("number",number); 185 num = db.update("car",values,"name = ?",new String[]{name}); 186 } catch (Exception e) { 187 e.printStackTrace(); 188 } finally { 189 if (db!=null){ 190 db.close(); 191 } 192 } 193 194 return num; 195 } 196 197 198 199 200 201 202 203 class MyBaseAdapter extends BaseAdapter { 204 205 public MyBaseAdapter(MainActivity mainActivity, int list_item, List<Sqldata> show) { 206 } 207 208 @Override 209 public int getCount() { 210 return list.size(); 211 } 212 213 @Override 214 public Object getItem(int position) { 215 return null; 216 } 217 218 @Override 219 public long getItemId(int position) { 220 return position; 221 } 222 223 @Override 224 public View getView(int position, View convertView, ViewGroup parent) { 225 View view = View.inflate(MainActivity.this,R.layout.item,null); 226 TextView tname = (TextView)view.findViewById(R.id.t_name); 227 TextView tprice = (TextView)view.findViewById(R.id.t_price); 228 TextView tnumber = (TextView)view.findViewById(R.id.t_number); 229 230 231 tname.setText(list.get(position).getName()); 232 tprice.setText(list.get(position).getPrice()); 233 tnumber.setText(list.get(position).getNumber()); 234 return view; 235 } 236 } 237 238 239 }
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context=".SecondActivity"> 8 9 <TextView 10 android:id="@+id/tn" 11 android:layout_width="150dp" 12 android:layout_height="wrap_content" /> 13 14 <TextView 15 android:id="@+id/tp" 16 android:layout_width="100dp" 17 android:layout_height="wrap_content" /> 18 19 <TextView 20 android:id="@+id/tnu" 21 android:layout_width="wrap_content" 22 android:layout_height="wrap_content" /> 23 </LinearLayout>