在清单文件AndroidManifest.xml中:
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"
package=".shopshowdemo">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
在layout中的activity_main.xml文件中写如下代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.shopshowdemo.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="商品名称"
android:inputType="text"
android:layout_weight="1"/>
<EditText
android:id="@+id/etAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="商品金额"
android:inputType="number"
android:layout_weight="1"/>
<ImageView
android:id="@+id/ivAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="addGoods"
android:src="@android:drawable/ic_input_add"/>
</LinearLayout>
<ListView
android:id="@+id/ivGoods"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
在layout中新建item.xml文件:
<?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">
<TextView
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvid"
android:layout_weight="1"
android:textSize="20sp"/>
<TextView
android:text="商品名称"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:layout_weight="1"
android:textSize="20sp"/>
<TextView
android:text="商品金额"
android:gravity="left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvAmount"
android:layout_weight="2"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/ivUp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_up_float"/>
<ImageView
android:id="@+id/ivDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"/>
</LinearLayout>
<ImageView
android:id="@+id/ivDelete"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_menu_delete"/>
</LinearLayout>
在第一个包下面的MainActivity.java中,写如下代码:
public class MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etAmount;
private ListView IvGoods;
private GoodsAdapter goodsAdapter;
private GoodsDao goodsDao;
private List<Goods> goodsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etName=(EditText)findViewById(R.id.etName);
etAmount=(EditText)findViewById(R.id.etAmount);
IvGoods=(ListView) findViewById(R.id.ivGoods);
goodsDao=new GoodsDao(this);
goodsList=goodsDao.queryAll();
goodsAdapter=new GoodsAdapter(this,R.layout.item,goodsList,goodsDao);
IvGoods.setAdapter(goodsAdapter);
}
public void addGoods(View view){
String name=etName.getText().toString();
String amount=etAmount.getText().toString();
if (TextUtils.isEmpty(name)||TextUtils.isEmpty(amount))
return;
Goods goods=new Goods(name,Integer.parseInt(amount));
goodsDao.add(goods);
goodsList.add(goods);
goodsAdapter.notifyDataSetChanged();
etName.setText("");
etAmount.setText("");
Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();
}
}
在第一个包下面新建GoodsAdapter类,在GoodsAdapter.java中写如下代码:
package com.example.administrator.shopshowdemo;
public class GoodsAdapter extends ArrayAdapter<Goods>{
private int resounceId;
private GoodsDao goodsDao;
private List<Goods>goodsList;
public GoodsAdapter(Contextcontext, int resource, List<Goods> objects,GoodsDao
goodsDao) {
super(context, resource,objects);
resounceId=resource;
goodsList=objects;
this.goodsDao=goodsDao;
}
@NonNull
@Override
public View getView(intposition, View convertView, ViewGroup parent) {
final Goodsgoods=getItem(position);
View view = null;
ViewHolder viewHolder;
if (convertView==null){
view =LayoutInflater.from(getContext()).inflate(R.layout.item,null);
viewHolder=newViewHolder();
viewHolder.tvId=(TextView) view.findViewById(R.id.tvid);
viewHolder.tvName=(TextView) view.findViewById(R.id.tvName);
viewHolder.tvAmount=(TextView) view.findViewById(R.id.tvAmount);
viewHolder.ivUp=(ImageView) view.findViewById(R.id.ivUp);
viewHolder.ivDown=(ImageView) view.findViewById(R.id.ivDown);
viewHolder.ivDelete=(ImageView)view.findViewById(R.id.ivDelete);
view.setTag(viewHolder);
}
else
{
view=convertView;
viewHolder=(ViewHolder) view.getTag();
}
viewHolder.tvId.setText(goods.getId()+"");
viewHolder.tvName.setText(goods.getName());
viewHolder.tvId.setText(goods.getAmount()+"");
viewHolder.ivDelete.setOnClickListener(new View.OnClickListener()
{
@Override
public voidonClick(View v)
{
AlertDialog.Builderbuilder=new AlertDialog.Builder(getContext());
builder.setTitle("你确定要删除吗?");
builder.setPositiveButton("yes", newDialogInterface.OnClickListener()
{
@Override
public voidonClick(DialogInterface dialogInterface, int which)
{
goodsList.remove(goods);
goodsDao.delete(goods.getId());
notifyDataSetChanged();
}
});
builder.setNegativeButton("cancel",null);
builder.show();
}
});
viewHolder.ivUp.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(Viewv)
{
goods.setAmount(goods.getAmount()-1);
goodsDao.update(goods);
notifyDataSetChanged();
}
});
viewHolder.ivDown.setOnClickListener(new View.OnClickListener()
{
@Override
public voidonClick(View v)
{
goods.setAmount(goods.getAmount()+1);
goodsDao.update(goods);
notifyDataSetChanged();
}
});
return view;
}
class ViewHolder
{
TextView tvId;
TextView tvName;
TextView tvAmount;
ImageView ivUp;
ImageView ivDown;
ImageView ivDelete;
}
}
在第一个包下面新建entity包,在此包下新建Goods类:
Goods.java
public class Goods {
private Long id;
private String name;
private Integer amount;
public Goods(Long id, Stringname, Integer amount) {
this.id = id;
this.name = name;
this.amount = amount;
}
public Goods(String name,Integer amount) {
this.name = name;
this.amount = amount;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(Stringname) {
this.name = name;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integeramount) {
this.amount = amount;
}
@Override
public String toString() {
return "Goods{"+
"id=" +id +
", name='" + name + '\'' +
",amount=" + amount +
'}';
}
}
在第一个包中新建db包,在此包下面新建DBHelper类。
DBHelper.Java:
public class DBHelper extends SQLiteOpenHelper{
public static final String CREATE_GOODS="create table goods(id integer primary
key autoincrement,name varchar(20),amount integer)";
public DBHelper(Contextcontext, int version) {
super(context,"goods.db", null , version);
}
@Override
public voidonCreate(SQLiteDatabase db) {
db.execSQL(CREATE_GOODS);
}
@Override
public voidonUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
在第一个包中新建dao包,在此包下面新建GoodDao类。
GoodsDao.java:
public class GoodsDao {
private DBHelper dbHelper;
public GoodsDao(Contextcontext){
dbHelper=newDBHelper(context,1);
}
public void add(Goods goods){
SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
ContentValues valuse= newContentValues();
valuse.put("name",goods.getName());
valuse.put("amount",goods.getAmount());
longid=sqLiteDatabase.insert("gooods",null,valuse);
goods.setId(id);
sqLiteDatabase.close();
}
public int delete(long id){
SQLiteDatabasesqLiteDatabase=dbHelper.getWritableDatabase();
intcount=sqLiteDatabase.delete("goods","_id-?",newString[]{id+""});
sqLiteDatabase.close();
return count;
}
public int update(Goodsgoods){
SQLiteDatabasesqLiteDatabase=dbHelper.getWritableDatabase();
ContentValues valuse= newContentValues();
valuse.put("name",goods.getName());
valuse.put("amount",goods.getAmount());
intcount=sqLiteDatabase.update("goods",valuse,"_id+?",newString[]
{goods.getId()+""});
sqLiteDatabase.close();
return count;
}
public List<Goods>queryAll(){
List<Goods>goodsList=new ArrayList<>();
SQLiteDatabasesqLiteDatabase=dbHelper.getReadableDatabase();
Cursorcursor=sqLiteDatabase.query("goods",null,null,null,null,null,"amount
desc");
while(cursor.moveToNext()){
long id=cursor.getLong(cursor.getColumnIndex("_id"));
Stringname=cursor.getString(cursor.getColumnIndex("name"));
intamount=cursor.getInt(cursor.getColumnIndex("amount"));
Goods goods=newGoods(id,name,amount);
goodsList.add(goods);
}
cursor.close();
sqLiteDatabase.close();
return goodsList;
}
}
在values下的colors.xml中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<colorname="colorPrimary">#3F51B5</color>
<colorname="colorPrimaryDark">#303F9F</color>
<colorname="colorAccent">#FF4081</color>
</resources>
在values下的dimens.xml中的代码:
<resources>
<!-- Default screenmargins, per the Android Design guidelines. -->
<dimenname="activity_horizontal_margin">16dp</dimen>
<dimenname="activity_vertical_margin">16dp</dimen>
</resources>
在values中的styles.xml中的代码:
<resources>
<!-- Base applicationtheme. -->
<stylename="AppTheme"parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize yourtheme here. -->
<itemname="colorPrimary">@color/colorPrimary</item>
<itemname="colorPrimaryDark">@color/colorPrimaryDark</item>
<itemname="colorAccent">@color/colorAccent</item>
</style>
</resources>