最后一次作业
package com.example.myapplication; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import java.util.ArrayList; import java.util.List; public class MainActivity extends AppCompatActivity implements View.OnClickListener { Intent intent = new Intent(); EditText name,price,number; Button add; ListView lv; private MyHelper helper; List<Sqldata> list = new ArrayList<>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name = (EditText)findViewById(R.id.et_1); price = (EditText)findViewById(R.id.et_2); number = (EditText)findViewById(R.id.et_3); add = (Button)findViewById(R.id.add); lv = (ListView)findViewById(R.id.list); helper = new MyHelper(this); } public void add_Click(View view){ Sqldata date = new Sqldata(name.getText().toString(),price.getText().toString(),number.getText().toString()); insert(date); Toast.makeText(MainActivity.this,name.getText().toString()+"已添加到购物车",Toast.LENGTH_SHORT).show(); } public void delete_Click(View view){ int num = delete(name.getText().toString()); if (num > 0){ Toast.makeText(MainActivity.this,"删除成功",Toast.LENGTH_SHORT).show(); }else{ Toast.makeText(MainActivity.this,"删除失败",Toast.LENGTH_SHORT).show(); } } public void update_Click(View view){ update(name.getText().toString(),price.getText().toString(),number.getText().toString()); } public void find_Click(View view){ find(name.getText().toString()); } @Override public void onClick(View v) { } //添加到购物车 public long insert(Sqldata sqldata){ long id = 0; SQLiteDatabase db =null; try { db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("name",sqldata.getName()); values.put("price",sqldata.getPrice()); values.put("number",sqldata.getNumber()); db.insert("car",null,values); } catch (Exception e) { e.printStackTrace(); } finally { if (db != null){ db.close(); } } return id; } //从购物车中删除 public int delete(String name){ SQLiteDatabase db = null; int num = 0; try { db = helper.getWritableDatabase(); num = db.delete("car","name = ?",new String[]{name}); } catch (Exception e) { e.printStackTrace(); } finally { if (db!=null){ db.close(); } } return num; } //查看购物车 public List<Sqldata> find(String name){ SQLiteDatabase db = null; db = helper.getReadableDatabase(); MyBaseAdapter myBaseAdapter = new MyBaseAdapter(this,R.layout.item,list); try { Cursor cursor = db.query("car",null,null,null,null,null,null); if (cursor.getCount()!=0){ while (cursor.moveToNext()){ Sqldata find = new Sqldata(); find.setName(cursor.getString(cursor.getColumnIndex("name"))); find.setPrice(cursor.getString(cursor.getColumnIndex("price"))); find.setNumber(cursor.getString(cursor.getColumnIndex("number"))); list.add(find); lv.setAdapter(myBaseAdapter); } } cursor.close(); } catch (Exception e) { e.printStackTrace(); } finally { if (db != null){ db.close(); } } return list; } //对购物车进行修改 public int update(String name,String price,String number){ SQLiteDatabase db = null; int num = 0; try { db = helper.getWritableDatabase(); ContentValues values = new ContentValues(); values.put("price",price); values.put("number",number); num = db.update("car",values,"name = ?",new String[]{name}); } catch (Exception e) { e.printStackTrace(); } finally { if (db!=null){ db.close(); } } return num; } class MyBaseAdapter extends BaseAdapter { public MyBaseAdapter(MainActivity mainActivity, int list_item, List<Sqldata> show) { } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return position; }
package com.example.myapplication; 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, "car.db", null, 1); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table car(_id integer primary key autoincrement,name varchar(50),price varchar(50),number varchar(50))"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; import android.widget.Toast; public class SecondActivity extends AppCompatActivity { Intent intent = new Intent(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); intent=getIntent(); String n = intent.getStringExtra("name"); String u = intent.getStringExtra("number"); Toast.makeText(SecondActivity.this,"数据已传送,姓名:"+n+"电话号码:"+u,Toast.LENGTH_SHORT).show(); } }
package com.example.myapplication; public class Sqldata { private int id; private String name,price,number; public Sqldata() { } public Sqldata(int id, String name, String price, String number) { this.id = id; this.name = name; this.price = price; this.number = number; } public Sqldata(String name, String price, String number) { this.name = name; this.price = price; this.number = number; } public int getId() { return id; } public void setId(int id) { this.id = id; } 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; } }
@Override public View getView(int position, View convertView, ViewGroup parent) { View view = View.inflate(MainActivity.this,R.layout.item,null); TextView tname = (TextView)view.findViewById(R.id.t_name); TextView tprice = (TextView)view.findViewById(R.id.t_price); TextView tnumber = (TextView)view.findViewById(R.id.t_number); tname.setText(list.get(position).getName()); tprice.setText(list.get(position).getPrice()); tnumber.setText(list.get(position).getNumber()); return view; } } }