<?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="@color/E_group_2" 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" style="@style/textViewStyle" android:text="名称:" /> <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" style="@style/textViewStyle" android:text="价格:" /> <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" style="@style/textViewStyle" android:text="数量:" /> <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_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="添加"/> <Button android:id="@+id/query" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="查询"/> <Button android:id="@+id/update" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="修改"/> <Button android:id="@+id/delete" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" android:text="删除"/> </LinearLayout> <LinearLayout android:background="#ffffff" android:layout_marginTop="10dp" android:padding="10dp" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:text="名称" style="@style/listViewStyle" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"></TextView> <TextView android:text="价格" style="@style/listViewStyle" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"></TextView> <TextView android:text="数量" style="@style/listViewStyle" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content"></TextView> </LinearLayout> <View android:layout_height="1px" android:layout_width="fill_parent"> </View> <ListView android:id="@+id/listView" android:layout_width="match_parent" android:layout_height="match_parent" 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="horizontal" android:padding="10dp"> <TextView android:id="@+id/tv_name" android:text="测试文本" android:layout_weight="1" style="@style/listViewStyle" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_price" android:text="测试文本" style="@style/listViewStyle" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> <TextView android:id="@+id/tv_number" android:text="测试文本" style="@style/listViewStyle" android:layout_weight="1" android:layout_width="0dp" android:layout_height="wrap_content" /> </LinearLayout>
public class Goods { private String name; private String price; private String count; 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 getCount() { return count; } public void setCount(String count) { this.count = count; } }
public class DbHelper extends SQLiteOpenHelper { SQLiteDatabase db; ContentValues contentValues=new ContentValues(); public DbHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) { super(context, name, factory, version); db=this.getWritableDatabase(); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL("create table data(id Integer primary key autoincrement,name text,price text,count text )"); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } public boolean add(String name, String price, String count) { contentValues.put("name",name); contentValues.put("price",price); contentValues.put("count",count); long flag=db.insert("data",null,contentValues); return flag>0?true:false; } public List<Goods> query() { List<Goods> list=new ArrayList<>(); Cursor cursor=db.query("data",null,null,null,null,null,null); if (cursor!=null){ while (cursor.moveToNext()){ Goods goods=new Goods(); goods.setName(cursor.getString(1)); goods.setPrice(cursor.getString(2)); goods.setCount(cursor.getString(3)); list.add(goods); } } return list; } public boolean update(String name, String price, String count) { contentValues.put("name",name); contentValues.put("price",price); contentValues.put("count",count); long flag=db.update("data",contentValues,"name=?",new String[]{name}); return flag>0?true:false; } public List<Goods> get(String name) { List<Goods> list=new ArrayList<>(); Cursor cursor=db.query("data",null,"name=?",new String[]{name},null,null,null); if (cursor!=null){ while (cursor.moveToNext()){ Goods goods=new Goods(); goods.setName(cursor.getString(1)); goods.setPrice(cursor.getString(2)); goods.setCount(cursor.getString(3)); list.add(goods); } } return list; } public boolean delete(String name) { long flag=db.delete("data","name=?",new String[]{name}); return flag>0?true:false; } }
public class MyAdapter extends BaseAdapter { private List<Goods> list; private LayoutInflater layoutInflater; public MyAdapter(List<Goods> list, Context context) { this.list=list; this.layoutInflater =LayoutInflater.from(context); } @Override public int getCount() { return list.size(); } @Override public Object getItem(int position) { return list.get(position); } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder=null; if (convertView==null){ viewHolder=new ViewHolder(); convertView=layoutInflater.inflate(R.layout.listview_item,null,false); viewHolder.tv_showCount=convertView.findViewById(R.id.tv_number); viewHolder.tv_showName=convertView.findViewById(R.id.tv_name); viewHolder.tv_showPrice=convertView.findViewById(R.id.tv_price); convertView.setTag(viewHolder); }else { viewHolder=(ViewHolder) convertView.getTag(); } Goods goods=(Goods) getItem(position); viewHolder.tv_showPrice.setText(goods.getPrice()); viewHolder.tv_showName.setText(goods.getName()); viewHolder.tv_showCount.setText(goods.getCount()); return convertView; } class ViewHolder{ TextView tv_showName,tv_showPrice,tv_showCount; } }
public class MainActivity extends AppCompatActivity implements View.OnClickListener{ private EditText et_name,et_price,et_count; private Button bt_add,bt_update,bt_delete,bt_query; private ListView lv_show; private DbHelper dbHelper; private String name,price,count; private List<Goods> goodsList; private MyAdapter myAdapter; private List<Goods> findList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); dbHelper=new DbHelper(MainActivity.this,"data.db",null,1); if (goodsList!=null){ goodsList.clear(); } goodsList=dbHelper.query(); myAdapter=new MyAdapter(goodsList,MainActivity.this); lv_show.setAdapter(myAdapter); } @Override public void onClick(View v) { switch (v.getId()){ case R.id.delete: name=et_name.getText().toString(); if (dbHelper.delete(name)){ showToast("删除成功"); showListView(); }else { showToast("删除失败"); } break; case R.id.add: name=et_name.getText().toString(); price=et_price.getText().toString(); count=et_count.getText().toString(); if (name==null){ showToast("名称不能为空!"); }else { if (dbHelper.add(name,price,count)){ showToast("添加成功"); showListView(); }else { showToast("添加失败"); } } break; case R.id.query: name=et_name.getText().toString(); if (findList!=null){ findList.clear(); } findList=dbHelper.get(name); myAdapter=new MyAdapter(findList,MainActivity.this); lv_show.setAdapter(myAdapter); break; case R.id.update: name=et_name.getText().toString(); price=et_price.getText().toString(); count=et_count.getText().toString(); if (dbHelper.update(name,price,count)){ showToast("修改成功"); showListView(); }else { showToast("修改失败,名称不能修改"); } break; } } private void init() { et_count=findViewById(R.id.et_number); et_name=findViewById(R.id.et_name); et_price=findViewById(R.id.et_price); bt_add=findViewById(R.id.add); bt_delete=findViewById(R.id.delete); bt_query=findViewById(R.id.query); bt_update=findViewById(R.id.update); lv_show=findViewById(R.id.listView); bt_add.setOnClickListener(this); bt_query.setOnClickListener(this); bt_delete.setOnClickListener(this); bt_update.setOnClickListener(this); } private void showToast(String msg){ Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show(); } private void showListView(){ goodsList=dbHelper.query(); myAdapter=new MyAdapter(goodsList,MainActivity.this); lv_show.setAdapter(myAdapter); } }