1、界面展示
商品展示界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/activity_exe" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="com.example.administrator.hello.Exe"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_marginTop="10dp" android:layout_marginLeft="10dp" android:layout_height="wrap_content" android:hint="商品名称"/> <TextView android:layout_width="0dp" android:layout_weight="1" android:layout_marginTop="10dp" android:layout_height="wrap_content" android:hint="金额"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="addgoods" app:srcCompat="@android:drawable/ic_input_add" android:id="@+id/imageView" /> </LinearLayout> </LinearLayout>
listview界面
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_id"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_name"/> <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tv_money"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/arrow_up_float" android:id="@+id/tv_up"/> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/arrow_down_float" android:id="@+id/tv_down"/> </LinearLayout> <ImageView android:layout_width="25dp" android:layout_height="25dp" android:src="@android:drawable/ic_menu_delete" android:id="@+id/tv_delete"/> </LinearLayout> <ListView android:id="@+id/ivgoods" android:layout_width="wrap_content" android:layout_height="wrap_content"> </ListView> </LinearLayout>
2、mainActivbity代码如下
主要定义了构造方法
ublic class Exe extends AppCompatActivity { private EditText etname; private EditText etmoney; private ListView ivgoods; private Goodsdao goooddao; private GoodsAdapter goodsAdapter; private List<Gooods> goodsList; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_exe); etname= (EditText) findViewById(R.id.tv_name); etmoney= (EditText) findViewById(R.id.tv_money); ivgoods= (ListView) findViewById(R.id.ivgoods); goooddao=new Goodsdao(this); goooddao.queryAll(); goodsAdapter=new GoodsAdapter(this,R.layout.listview,goodsList); } public void addgoods(View view){ String name=etname.getText().toString(); String money=etmoney.getText().toString(); Gooods gooods=new Gooods(name,money,equals("")?0:Integer.parseInt(money)); goooddao.add(gooods); goodsAdapter.notifyDataSetChanged(); Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show(); } }3、商品各个变量的定义
public class Gooods { private Long id; private String name; private String money; public Gooods(Long id, String name, Integer amount) { this.id = id; this.name = name; this.money = money; } public Gooods(String money, String name) { this.money = money; this.name = name; } public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getMoney() { return money; } public void setMoney(String money) { this.money = money; } @Override public String toString() { return "Gooods{" + "id=" + id + ", name='" + name + '\'' + ", amount=" + money + '}'; } }4、建立适配器GoodsAdapter
public class GoodsAdapter extends ArrayAdapter<Gooods>{ private int resourceid; public GoodsAdapter(Context context, int resource,List<Gooods> objects) { super(context, resource, objects); resourceid=resource; } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { Gooods gooods=getItem(position); //获取当前位置对应的商品 View view=null; ViewHolder viewHolder; if (convertView==null){ view=LayoutInflater.from(getContext()).inflate(R.layout.listview,null); viewHolder=new ViewHolder(); viewHolder.tv_id= (TextView) view.findViewById(R.id.tv_id); viewHolder.tv_name= (TextView) view.findViewById(R.id.tv_name); viewHolder.tv_money= (TextView) view.findViewById(R.id.tv_money); viewHolder.tv_up= (ImageView) view.findViewById(R.id.tv_up); viewHolder.tv_down= (ImageView) view.findViewById(R.id.tv_down); viewHolder.tv_delete= (ImageView) view.findViewById(R.id.tv_delete); view.setTag(viewHolder); } else { view=convertView; viewHolder= (ViewHolder) view.getTag(); } viewHolder.tv_id.setText(gooods.getId()+""); viewHolder.tv_name.setText(gooods.getName()); viewHolder.tv_money.setText(gooods.getMoney()+""); return view; } class ViewHolder{ TextView tv_id; TextView tv_name; TextView tv_money; ImageView tv_up; ImageView tv_down; ImageView tv_delete; } }5、数据库连接
public class DBHelper extends SQLiteOpenHelper { public static final String CREATE_GOODS="create table goods(_id integer primary key autoincrement,name varchar(20),money integer)"; public DBHelper(Context context, int version) { super(context, "goods.db", null, version); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_GOODS); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } }6、数据的增删改查
public class Goodsdao { private DBHelper dbHelper; public Goodsdao(Context context){ dbHelper=new DBHelper(context,1); } public void add(Gooods goods){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",goods.getName()); values.put("money",goods.getMoney()); sqLiteDatabase.insert("goods",null,values); sqLiteDatabase.close(); } public int delete(long id){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); int count= sqLiteDatabase.delete("goods","_id=?",new String[]{id+""}); sqLiteDatabase.close(); return count; } public int update(Gooods goods){ SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase(); ContentValues values=new ContentValues(); values.put("name",goods.getName()); values.put("money",goods.getMoney()); int count=sqLiteDatabase.update("goods",values,"_id=?",new String[]{goods.getId()+""}); sqLiteDatabase.close(); return count; } public List<Gooods>queryAll(){ List<Gooods> gooodsList=new ArrayList<>(); SQLiteDatabase sqLiteOpenHelper=dbHelper.getReadableDatabase(); Cursor cursor=sqLiteOpenHelper.query("goods",null,null,null,null,null,"money desc"); while (cursor.moveToNext()){ long id=cursor.getLong(cursor.getColumnIndex("id")); String name=cursor.getString(cursor.getColumnIndex("name")); int money=cursor.getInt(cursor.getColumnIndex("money")); Gooods gooods=new Gooods(id,name,money); gooodsList.add(gooods); } cursor.close(); sqLiteOpenHelper.close(); return gooodsList; } }二、运行效果图
如图添加了一个商品 我们再添加一个看看
如图 2b铅笔 5元 现在我们删除这个商品
点击确定后】