<?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" 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="42dp" android:orientation="horizontal"> <ImageView android:id="@+id/imageView6" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="1dp" android:layout_marginTop="11dp" app:srcCompat="@drawable/caidan" /> <TextView android:id="@+id/textView" android:layout_width="174dp" android:layout_height="35dp" android:layout_marginLeft="79dp" android:layout_marginTop="5dp" android:text="OnlineStore" android:textColor="#010102" android:textSize="30sp" android:textStyle="bold|italic" /> <ImageView android:id="@+id/imageView7" android:layout_width="30dp" android:layout_height="30dp" android:layout_marginLeft="70dp" android:layout_marginTop="11dp" app:srcCompat="@drawable/sousuo" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="62dp" android:orientation="horizontal"> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" app:srcCompat="@drawable/shouyesix" /> </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="18sp" android:textStyle="bold|italic" /> <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="18sp" android:textStyle="bold|italic" /> <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="18sp" android:textStyle="bold|italic" /> <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:layout_weight="1" android:background="#191A24" android:text="添加" android:textColor="#FFFFFF" android:textSize="16sp" android:textStyle="bold" /> <Button android:id="@+id/query" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#FDFDFE" android:text="查询" android:textColor="#191A24" android:textSize="16sp" android:textStyle="bold" /> <Button android:id="@+id/update" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#191A24" android:text="修改" android:textColor="#FFFFFF" android:textSize="16sp" android:textStyle="bold" /> <Button android:id="@+id/delete" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:background="#FDFDFE" android:text="删除" android:textColor="#191A24" android:textSize="16sp" android:textStyle="bold" /> </LinearLayout> <ListView android:id="@+id/listView" android:layout_width="wrap_content" android:layout_height="262dp" android:background="#ffffff"> </ListView> <LinearLayout android:layout_width="match_parent" android:layout_height="89dp" android:orientation="horizontal"> <Button android:id="@+id/button5" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:background="#191A24" android:onClick="Zhuan" android:text="首页" android:textColor="#FDFDFE" android:textSize="20sp" android:textStyle="bold|italic" /> <Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:background="#FDFDFE" android:text="购物" android:textSize="20sp" android:textStyle="bold|italic" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="50dp" android:layout_weight="1" android:background="#191A24" android:onClick="Zhuan" android:text="资讯" android:textColor="#FDFDFE" android:textSize="20sp" android:textStyle="bold|italic" /> </LinearLayout> </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"/> <ImageView android:id="@+id/imageView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:src="@drawable/img" /> </LinearLayout>
package com.example.onlinestore; import android.content.ContentValues; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.support.v7.app.AppCompatActivity; 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 ShoppingActivity extends AppCompatActivity 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_shopping); et_name = (EditText)findViewById(R.id.et_name); et_price = (EditText)findViewById(R.id.et_price); et_number = (EditText) findViewById(R.id.et_number); listView = (ListView)findViewById(R.id.listView); Button add = (Button)findViewById(R.id.add); Button query =(Button) findViewById(R.id.query); Button update =(Button) findViewById(R.id.update); Button delete = (Button)findViewById(R.id.delete); add.setOnClickListener(this); query.setOnClickListener(this); update.setOnClickListener(this); delete.setOnClickListener(this); myHelper = new MyHelper(this); } public void Zhuan(View v) { switch (v.getId()) { case R.id.button5: Intent intent = new Intent(ShoppingActivity.this, ShouOneActivity.class); startActivity(intent); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); finish(); break; case R.id.button3: Intent intent3 = new Intent(ShoppingActivity.this, ConsultActivity.class); startActivity(intent3); overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out); finish(); break; } } // @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(); //创建ContentValues对象 values.put("name", name); //将数据添加到ContentValues对象 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(ShoppingActivity.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(ShoppingActivity.this,list2); listView.setAdapter(adapter); adapter.notifyDataSetChanged(); db.close(); Toast.makeText(this, "信息已删除", Toast.LENGTH_SHORT).show(); break; } } } package com.example.onlinestore; 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.onlinestore; import android.content.Context; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; import java.util.List; public class CartAdapter extends BaseAdapter { private List<CartBean> list; private LayoutInflater layoutInflater; public CartAdapter(Context context, List<CartBean> list){ this.layoutInflater = LayoutInflater.from(context); this.list = list; } @Override public int getCount() { Log.e("yanwenhua","list.size()--"+list.size()); return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (convertView==null){ convertView=layoutInflater.inflate(R.layout.listview_item,null); viewHolder=new ViewHolder(convertView); convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder) convertView.getTag(); } CartBean cartBean = list.get(position); viewHolder.tv_name.setText("商品名称:"+cartBean.getName()); viewHolder.tv_price.setText("商品价格:"+cartBean.getPrice()); viewHolder.tv_number.setText("商品数量:"+cartBean.getNumber()); Log.e("yanwenhua","cartBean.getName()-"+cartBean.getName()+" "+cartBean.getPrice()+" "+cartBean.getNumber()); return convertView; } class ViewHolder{ TextView tv_name; TextView tv_price; TextView tv_number; public ViewHolder(View view){ tv_name = (TextView) view.findViewById(R.id.tv_name); tv_price = (TextView) view.findViewById(R.id.tv_price); tv_number = (TextView) view.findViewById(R.id.tv_number); } } } package com.example.onlinestore; public class CartBean { private String name; private String price; private String number; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } public String getNumber() { return number; } public void setNumber(String number) { this.number = number; } }