商品展示用户交互界面:
<?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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bz0209.showshopdemo.MainActivity">
<LinearLayout
android:id="@+id/addLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="@+id/nameET"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="商品名称"
android:inputType="textPersonName"
/>
<EditText
android:id="@+id/balanceET"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="金额"
android:inputType="number"
/>
<ImageView
android:onClick="add"
android:id="@+id/addIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/ic_input_add"
/>
<ListView
android:id="@+id/accountLV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/addLL">
</ListView>
</LinearLayout>>
</LinearLayout>
添加了三个TextView用于展示数据库中的某条数据的id、商品名称、金额:
![这里写图片描述](https://img-blog.csdn.net/20170508210800571?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc3MzgyMTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
<TextView
android:id="@+id/tvId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="商品名称"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="@+id/tvAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="金额"
android:textColor="#000000"
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:layout_marginBottom="2dp"
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" />
![这里写图片描述](https://img-blog.csdn.net/20170508210404487?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc3MzgyMTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
![这里写图片描述](https://img-blog.csdn.net/20170508210610615?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc3MzgyMTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
创建数据库:
DBHelper.java:
package com.example.bz0209.showshopdemo.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) {
}
}
界面显示如:
![这里写图片描述](https://img-blog.csdn.net/20170508210655211?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc3MzgyMTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
GoodsAdapter.java:
package com.example.bz0209.showshopdemo.entity;
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 com.example.bz0209.showshopdemo.R;
import java.util.List;
/**
* Created by Administrator on 2017/4/28.
*/
public class GoodsAdapter extends ArrayAdapter{
private int resounceId;
public GoodsAdapter(Context context, int resource, List objects) {
super(context, resource, objects);
resounceId=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(resounceId,null);
viewHolder=new ViewHolder();
viewHolder.tvName= (TextView) view.findViewById(R.id.tvName);
viewHolder.tvId= (TextView) view.findViewById(R.id.tvId);
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 com.example.bz0209.showshopdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.example.bz0209.showshopdemo.dao.GoodsDao;
import com.example.bz0209.showshopdemo.entity.Goods;
import com.example.bz0209.showshopdemo.entity.GoodsAdapter;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private EditText etName;
private EditText etAmount;
private ListView lvGoods;
private GoodsAdapter goodsAdapter;
private GoodsDao goodsDao;
private List 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.queryAll();
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();
Goods goods=new Goods(name,amount.equals("")?0:Integer.parseInt(amount));
goodsDao.add(goods);
}
}
![这里写图片描述](https://img-blog.csdn.net/20170508210850821?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcXFfMzc3MzgyMTg=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
package com.example.bz0209.showshopdemo.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import com.example.bz0209.showshopdemo.db.DBHelper;
import com.example.bz0209.showshopdemo.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());
sqLiteDatabase.insert(“goods”,null,values);
sqLiteDatabase.close();
}
}
package com.example.bz0209.showshopdemo.entity;
/**
* Created by Administrator on 2017/4/28.
*/
public class Goods {
private Long id;
private String name;
private Integer amount;
public Goods(Long id, String name, 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(String name) {
this.name = name;
}
public Integer getAmount() {
return amount;
}
public void setAmount(Integer amount) {
this.amount = amount;
}
@Override
public String toString() {
return "Goods{" +
"id=" + id +
", name='" + name + '\'' +
", amount=" + amount +
'}';
}
}
“`