1.代码
package cn.edu.bzu.shopshow.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import cn.edu.bzu.shopshow.db.DBHelper;
import cn.edu.bzu.shopshow.entity.Goods;
/**
* Created by Administrator on 2017/4/28.
*/
public class GoodsDao {
private DBHelper dbHelper;
public GoodsDao(Context context){
dbHelper = new DBHelper(context,1);
}
public void add(Goods goods){
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",goods.getName());
values.put("amount",goods.getAmount());
long id = sqLiteDatabase.insert("goods",null,values);
goods.setId(id);
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(Goods goods){
int count;
SQLiteDatabase sqLiteDatabase = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("name",goods.getName());
values.put("amount",goods.getAmount());
count =sqLiteDatabase.update("goods",values,"_id=?",new String[]{goods.getId()+""});
sqLiteDatabase.close();
return count;
}
public List<Goods> quertALL(){
List<Goods> goodsList = new ArrayList<>();
SQLiteDatabase sqLiteDatabase = dbHelper.getReadableDatabase();
Cursor cursor = sqLiteDatabase.query("goods",null,null,null,null,null,"amount desc");
while (cursor.moveToNext()){
long id = cursor.getLong(cursor.getColumnIndex("_id"));
String name = cursor.getString(cursor.getColumnIndex("name"));
int amount = cursor.getInt(cursor.getColumnIndex("amount"));
Goods goods = new Goods(id,amount,name);
goodsList.add(goods);
}
cursor.close();
sqLiteDatabase.close();
return goodsList;
}
}
package cn.edu.bzu.shopshow.db;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Administrator on 2017/4/28.
*/
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(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 sqLiteDatabase, int i, int i1) {
}
}
package cn.edu.bzu.shopshow;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;
import cn.edu.bzu.shopshow.entity.Goods;
/**
* Created by Administrator on 2017/4/28.
*/
public class GoodsAdapter extends ArrayAdapter<Goods>{
private int resourceId;
public GoodsAdapter(Context context, int resource, List<Goods> objects) {
super(context, resource, objects);
resourceId = resource;
}
@NonNull
@Override
public View getView(int position, View convertView, ViewGroup parent) { //对应数据放到相应空间上
Goods goods = getItem(position);
View view = null;
ViewHolder viewHolder;
if(convertView==null){
view = LayoutInflater.from(getContext()).inflate(R.layout.item,null);
viewHolder = new ViewHolder();
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.tvAmount.setText(goods.getAmount()+" ");
return view;
}
class ViewHolder{
TextView tvId;
TextView tvName;
TextView tvAmount;
ImageView ivUp;
ImageView ivDown;
ImageView ivDelete;
}
}
package cn.edu.bzu.shopshow.entity;
/**
* Created by Administrator on 2017/4/28.
*/
public class Goods {
private Long id;
private String name;
private int amount;
public Goods(long id, int amount, String name) {
this.id = id;
this.amount = amount;
this.name = name;
}
public Goods(String name, int 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(String name) {
this.name = name;
}
public int getAmount() {
return amount;
}
public void setAmount(int amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Goods{" + "id=" + id + ", name='" + name + '\'' + ", amount=" + amount + '}';
}
}
package cn.edu.bzu.shopshow;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import java.util.List;
import cn.edu.bzu.shopshow.dao.GoodsDao;
import cn.edu.bzu.shopshow.entity.Goods;
public class MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etAmount;
private ListView lvGoods;
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);
lvGoods = (ListView) findViewById(R.id.lvGoods);
goodsDao = new GoodsDao(this);
goodsList = goodsDao.quertALL();
goodsAdapter = new GoodsAdapter(this,R.layout.item,goodsList);
lvGoods.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,amount.equals("")?0:Integer.parseInt(amount));
Goods goods = new Goods(name,Integer.parseInt(amount));
goodsDao.add(goods);
goodsAdapter.notifyDataSetChanged();
goodsList.add(goods);
etName.setText("");
etAmount.setText("");
Toast.makeText(this,"添加成功",Toast.LENGTH_LONG);
}
}
<?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:textSize="20sp"
android:gravity="left"
android:layout_weight="1" />
<TextView
android:text="商品名称"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvName"
android:gravity="left"
android:textSize="20sp"
android:maxLines="1"
android:layout_weight="1"/>
<TextView
android:text="商品金额"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tvAmount"
android:gravity="left"
android:textSize="20sp"
android:maxLines="1"
android:layout_weight="1"/>
<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>
<?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="cn.edu.bzu.shopshow.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/lvGoods"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
2.效果图
3.结构