Android购物车
1
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 android:background="#E6E6E6" 6 android:orientation="vertical" 7 android:padding="10dp"> 8 <LinearLayout 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:layout_marginTop="15dp" 12 android:background="@android:color/white" 13 android:orientation="horizontal"> 14 <TextView 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:padding="10dp" 18 android:text="名称:" 19 android:textColor="#000" 20 android:textSize="20sp" /> 21 <EditText 22 android:id="@+id/et_name" 23 android:layout_width="match_parent" 24 android:layout_height="wrap_content" 25 android:layout_marginLeft="5dp" 26 android:background="@null" 27 android:padding="10dp" 28 android:maxLines="1"/> 29 </LinearLayout> 30 <LinearLayout 31 android:layout_width="match_parent" 32 android:layout_height="wrap_content" 33 android:layout_marginTop="15dp" 34 android:background="@android:color/white" 35 android:orientation="horizontal"> 36 <TextView 37 android:layout_width="wrap_content" 38 android:layout_height="wrap_content" 39 android:padding="10dp" 40 android:text="价格:" 41 android:textColor="#000" 42 android:textSize="20sp" /> 43 <EditText 44 android:id="@+id/et_price" 45 android:layout_width="match_parent" 46 android:layout_height="wrap_content" 47 android:layout_marginLeft="5dp" 48 android:background="@null" 49 android:padding="10dp" 50 android:maxLines="1"/> 51 </LinearLayout> 52 <LinearLayout 53 android:layout_width="match_parent" 54 android:layout_height="wrap_content" 55 android:layout_marginTop="10dp" 56 android:background="@android:color/white" 57 android:orientation="horizontal"> 58 <TextView 59 android:layout_width="wrap_content" 60 android:layout_height="wrap_content" 61 android:padding="10dp" 62 android:text="数量:" 63 android:textColor="#000" 64 android:textSize="20sp" /> 65 <EditText 66 android:id="@+id/et_number" 67 android:layout_width="match_parent" 68 android:layout_height="wrap_content" 69 android:layout_marginLeft="5dp" 70 android:background="@null" 71 android:padding="10dp" 72 android:maxLines="1"/> 73 </LinearLayout> 74 <LinearLayout 75 android:layout_width="match_parent" 76 android:layout_height="wrap_content" 77 android:orientation="horizontal" 78 android:layout_marginTop="10dp"> 79 <Button 80 android:id="@+id/add" 81 android:layout_width="wrap_content" 82 android:layout_height="wrap_content" 83 android:text="添加"/> 84 <Button 85 android:id="@+id/query" 86 android:layout_width="wrap_content" 87 android:layout_height="wrap_content" 88 android:text="查询"/> 89 <Button 90 android:id="@+id/update" 91 android:layout_width="wrap_content" 92 android:layout_height="wrap_content" 93 android:text="修改"/> 94 <Button 95 android:id="@+id/delete" 96 android:layout_width="wrap_content" 97 android:layout_height="wrap_content" 98 android:text="删除"/> 99 </LinearLayout> 100 <ListView 101 android:id="@+id/listView" 102 android:layout_width="wrap_content" 103 android:layout_height="wrap_content" 104 android:background="#ffffff"> 105 </ListView> 106 </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 android:orientation="vertical" 6 android:padding="10dp"> 7 <TextView 8 android:id="@+id/tv_name" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:textSize="15sp"/> 12 <TextView 13 android:id="@+id/tv_price" 14 android:layout_width="wrap_content" 15 android:layout_height="wrap_content" 16 android:layout_marginTop="3dp" 17 android:textSize="15sp"/> 18 <TextView 19 android:id="@+id/tv_number" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:layout_marginTop="3dp" 23 android:textSize="15sp"/> 24 25 </LinearLayout>
1 package com.example.myapplication; 2 3 import android.app.Activity; 4 import android.content.ContentValues; 5 import android.database.Cursor; 6 import android.database.sqlite.SQLiteDatabase; 7 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.EditText; 13 import android.widget.ListView; 14 import android.widget.Toast; 15 16 import java.util.ArrayList; 17 import java.util.List; 18 19 public class MainActivity extends Activity implements View.OnClickListener { 20 private EditText et_name,et_price,et_number; 21 private ListView listView; 22 private String name,price,number; 23 private MyHelper myHelper; 24 private SQLiteDatabase db; 25 @Override 26 protected void onCreate(Bundle savedInstanceState) { 27 super.onCreate(savedInstanceState); 28 setContentView(R.layout.activity_main); 29 et_name = findViewById(R.id.et_name); 30 et_price = findViewById(R.id.et_price); 31 et_number = findViewById(R.id.et_number); 32 listView = findViewById(R.id.listView); 33 Button add = findViewById(R.id.add); 34 Button query = findViewById(R.id.query); 35 Button update = findViewById(R.id.update); 36 Button delete = findViewById(R.id.delete); 37 add.setOnClickListener(this); 38 query.setOnClickListener(this); 39 update.setOnClickListener(this); 40 delete.setOnClickListener(this); 41 myHelper = new MyHelper(this); 42 } 43 44 @Override 45 public void onClick(View v) { 46 switch (v.getId()){ 47 case R.id.add: 48 db = myHelper.getWritableDatabase(); 49 name = et_name.getText().toString(); 50 price = et_price.getText().toString(); 51 number = et_number.getText().toString(); 52 ContentValues values = new ContentValues(); 53 values.put("name", name); 54 values.put("price", price); 55 values.put("number", number); 56 db.insert("cart", null, values); 57 db.close(); 58 Toast.makeText(this, "信息已添加", Toast.LENGTH_SHORT).show(); 59 break; 60 case R.id.query: 61 Log.e("yanwenhua","123"); 62 List<CartBean> list = new ArrayList<>(); 63 db = myHelper.getWritableDatabase(); 64 Cursor cursor = db.query("cart", null, null, null, null, 65 null, null); 66 if (cursor.getCount() == 0) { 67 Toast.makeText(this, "没有数据", Toast.LENGTH_SHORT).show(); 68 } else { 69 while (cursor.moveToNext()) { 70 CartBean cartBean = new CartBean(); 71 int nameIndex = cursor.getColumnIndex("name"); 72 int priceIndex = cursor.getColumnIndex("price"); 73 int numberIndex = cursor.getColumnIndex("number"); 74 String name = cursor.getString(nameIndex); 75 String price = cursor.getString(priceIndex); 76 String number = cursor.getString(numberIndex); 77 Log.e("yanwenhua","cursor.getCount();--"+cursor.getCount()+"name-"+name+" "+price+" "+number); 78 cartBean.setName(name); 79 cartBean.setPrice(price); 80 cartBean.setNumber(number); 81 list.add(cartBean); 82 } 83 CartAdapter adapter = new CartAdapter(MainActivity.this,list); 84 listView.setAdapter(adapter); 85 adapter.notifyDataSetChanged(); 86 } 87 cursor.close(); 88 db.close(); 89 break; 90 case R.id.update: 91 name = et_name.getText().toString(); 92 price = et_price.getText().toString(); 93 number = et_number.getText().toString(); 94 db = myHelper.getWritableDatabase(); 95 values = new ContentValues(); 96 values.put("number",number); 97 values.put("price",price); 98 db.update("cart", values, "name=?", 99 new String[]{name}); 100 db.close(); 101 Toast.makeText(this, "信息已修改", Toast.LENGTH_SHORT).show(); 102 break; 103 case R.id.delete: 104 db = myHelper.getWritableDatabase(); 105 db.delete("cart", null, null); 106 List<CartBean> list2 = new ArrayList<>(); 107 CartAdapter adapter = new CartAdapter(MainActivity.this,list2); 108 listView.setAdapter(adapter); 109 adapter.notifyDataSetChanged(); 110 db.close(); 111 Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show(); 112 break; 113 } 114 115 } 116 }
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 class MyHelper extends SQLiteOpenHelper { 8 public MyHelper(Context context) { 9 super(context, "shoppingcart.db", null, 1); 10 } 11 @Override 12 public void onCreate(SQLiteDatabase db) { 13 db.execSQL("CREATE TABLE cart(_id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR(20), price VARCHAR(20), number VARCHAR(20))"); 14 } 15 @Override 16 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 17 } 18 }
1 package com.example.myapplication; 2 public class CartBean { 3 private String name; 4 private String price; 5 private String number; 6 7 public String getName() { 8 return name; 9 } 10 11 public void setName(String name) { 12 this.name = name; 13 } 14 15 public String getPrice() { 16 return price; 17 } 18 19 public void setPrice(String price) { 20 this.price = price; 21 } 22 23 public String getNumber() { 24 return number; 25 } 26 27 public void setNumber(String number) { 28 this.number = number; 29 } 30 }
1 package com.example.myapplication; 2 3 import android.content.Context; 4 import android.util.Log; 5 import android.view.LayoutInflater; 6 import android.view.View; 7 import android.view.ViewGroup; 8 import android.widget.BaseAdapter; 9 import android.widget.TextView; 10 11 import java.util.List; 12 13 public class CartAdapter extends BaseAdapter { 14 private List<CartBean> list; 15 private LayoutInflater layoutInflater; 16 public CartAdapter(Context context, List<CartBean> list){ 17 this.layoutInflater = LayoutInflater.from(context); 18 this.list = list; 19 } 20 @Override 21 public int getCount() { 22 Log.e("yanwenhua","list.size()--"+list.size()); 23 return list.size(); 24 } 25 26 @Override 27 public Object getItem(int position) { 28 return null; 29 } 30 31 @Override 32 public long getItemId(int position) { 33 return 0; 34 } 35 36 @Override 37 public View getView(int position, View convertView, ViewGroup parent) { 38 ViewHolder viewHolder; 39 if (convertView==null){ 40 convertView=layoutInflater.inflate(R.layout.listview_item,null); 41 viewHolder=new ViewHolder(convertView); 42 convertView.setTag(viewHolder); 43 }else { 44 viewHolder=(ViewHolder) convertView.getTag(); 45 } 46 CartBean cartBean = list.get(position); 47 viewHolder.tv_name.setText("商品名称:"+cartBean.getName()); 48 viewHolder.tv_price.setText("商品价格:"+cartBean.getPrice()); 49 viewHolder.tv_number.setText("商品数量:"+cartBean.getNumber()); 50 Log.e("yanwenhua","cartBean.getName()-"+cartBean.getName()+" "+cartBean.getPrice()+" "+cartBean.getNumber()); 51 return convertView; 52 } 53 class ViewHolder{ 54 TextView tv_name; 55 TextView tv_price; 56 TextView tv_number; 57 public ViewHolder(View view){ 58 tv_name = (TextView) view.findViewById(R.id.tv_name); 59 tv_price = (TextView) view.findViewById(R.id.tv_price); 60 tv_number = (TextView) view.findViewById(R.id.tv_number); 61 } 62 } 63 64 }