1.activity_mian.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: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="cn.edu.bzu.shangpinzhanshi.MainActivity"
android:weightSum="1">
<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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addIV"
android:src="@android:drawable/ic_input_add"/>
</LinearLayout>
<ListView
android:id="@+id/accountLV"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/addLL">
</ListView>
</LinearLayout>
2.创建ListView Item 布局
<?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:id="@+id/idTV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="13"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="@+id/nameTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:text="PQ"
android:textColor="#000000"
android:textSize="20sp"/>
<TextView
android:id="@+id/balanceTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:text="1234"
android:textColor="#000000"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/upIV"
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/downIV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"/>
</LinearLayout>
<ImageView
android:id="@+id/deleteIV"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_menu_delete"/>
</LinearLayout>
3.创建数据库
package cn.edu.bzu.my.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.Settings;
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context){
super(context,"item.db",null,2);
}
public void onCreata(SQLiteDatabase db){
System.out.println("onUpgrade");
db.execSQL("CREATE TABLE ACCOUNT(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTEGER)");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
System.out.println("onUpgrade");
}
}
4.创建Account类
package cn.edu.bzu.myapplication.bean;
public class Account {
private Long id;
private String name;
private int price;
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 getPrice(){
return price;
}
public void setPrice(int price){
this.price=price;
}
public Account(Long id, String name, int price){
super();
this.id=id;
this.name=name;
this.price=price;
}
public Account(String name, int price){
super();
this.name=name;
this.price=price;
}
public Account(){
super();
}
public String toString(){
return "(序号:"+id+",商品名称:"+name+",余额:"+price+")";
}
}
5.创建AccountDao类
package cn.edu.bzu.myapplication.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.myapplication.bean.Account;
public class AccountDao {
private MyHelper helper;
public AccountDao(Context context){
helper= new MyHelper(context) {
@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
}
};
}
public void insert(Account account){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("price",account.getPrice());
long id=db.insert("account",null,values);
account.setId(id);
db.close();
}
public int delete(long id){
SQLiteDatabase db=helper.getWritableDatabase();
int count=db.delete("account","_id=?",new String [] { id+""});
db.close();
return count;
}
public int update(Account account){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("price",account.getPrice());
int count=db.update("account",values,"_id=?",new String[]{ account.getId()+""});
db.close();
return count;
}
public List <Account> queryAll(){
SQLiteDatabase db=helper.getReadableDatabase();
Cursor c=db.query("account",null,null,null,null,null,"price DESC");
List<Account> list= new ArrayList<>();
while(c.moveToNext()){
long id=c.getLong(c.getColumnIndex("_id"));
String name=c.getString(1);
int price=c.getInt(2);
list.add(new Account(id,name,price));
}
c.close();
db.close();
return list;
}
}
6.MainActivity
package cn.edu.bzu.myapplication;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.List;
import cn.edu.bzu.myapplication.bean.Account;
import cn.edu.bzu.myapplication.dao.AccountDao;
public class MainActivity extends AppCompatActivity {
private List<Account> list;
private AccountDao dao;
private EditText nameET;
protected EditText priceET;
private MyAdapter adapter;
private ListView accountLV;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
dao=new AccountDao(this);
list=dao.queryAll();
adapter=new MyAdapter();
accountLV.setAdapter(adapter);
}
private void initView(){
accountLV=(ListView) findViewById(R.id.accountLV);
nameET=(EditText) findViewById(R.id.nameET);
priceET=(EditText) findViewById(R.id.priceET);
accountLV.setOnItemClickListener((AdapterView.OnItemClickListener) new MyOnItemClickListener());
}
public void add(View v){
String name=nameET.getText().toString().trim();
String price=priceET.getText().toString().trim();
Account a=new Account(name,price.equals("")?0:Integer.parseInt(price));
dao.insert(a);
list.add(a);
adapter.notifyDataSetChanged();
accountLV.setSelection(accountLV.getCount()-1);
nameET.setText("");
priceET.setText("");
}
private class MyAdapter extends BaseAdapter{
public int getCount(){
return list.size();
}
public Object getItem(int position){
return list.get(position);
}
public long getItemId(int position){
return position;
}
public View getView(int position,View convertView,ViewGroup parent){
View item=convertView!=null?convertView:View.inflate(
getApplicationContext(),R.layout.item,null);
TextView idTV=(TextView) item.findViewById(R.id.idTV);
TextView nameTV=(TextView) item.findViewById(R.id.nameTV);
TextView price=(TextView) item.findViewById(R.id.balanceTV);
final Account a=list.get(position);
idTV.setText(a.getId()+"");
nameTV.setText(a.getName());
price.setText(a.getPrice()+"");
ImageView upIV=(ImageView) item.findViewById(R.id.upIV);
ImageView downIV=(ImageView) item.findViewById(R.id.downIV);
ImageView deleteIV=(ImageView) item.findViewById(R.id.deleteIV);
upIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.setPrice(a.getPrice()-1);
notifyDataSetChanged();
dao.update(a);
}
});
deleteIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OnClickListener listener =
new OnClickListener() {
public void onCancel(DialogInterface dialogInterface) {
}
public void onClick(DialogInterface dialogInterface, int which) {
list.remove(a);
dao.delete(a.getId());
notifyDataSetChanged();
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("确定要删除吗?");
builder.setPositiveButton("确定",listener);
builder.setNegativeButton("取消",null);
builder.show();
}
});
return item;
}
}
private class MyOnItemClickListener implements AdapterView.OnItemClickListener {
public void onItemClick(AdapterView<?>parent,View view,int position,long id){
Account a=(Account) parent.getItemAtPosition(position);
Toast.makeText(getApplicationContext(),a.toString(),Toast.LENGTH_SHORT).show();
}
}
}