效果图
![这里写图片描述](https://img-blog.csdn.net/20170428100949275?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva19rb3JvbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
添加数据之后
![这里写图片描述](https://img-blog.csdn.net/20170428101114338?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva19rb3JvbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
布局文件
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"
android:layout_margin="8dp"
tools:context="cn.edu.bzu.product.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/addTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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>
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="wrap_content"
android:orientation="horizontal"
android:padding="10dp">
<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="0dp"
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="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="12345"
android:textColor="#000000"
android:textSize="20sp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/upTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_up_float"/>
<ImageView
android:id="@+id/downTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"/>
</LinearLayout>
<ImageView
android:id="@+id/deleteTV"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_menu_delete"/>
</LinearLayout>
![这里写图片描述](https://img-blog.csdn.net/20170428101437135?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva19rb3JvbA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
Account.class
package cn.edu.bzu.product.bean;
/**
* Created by Administrator on 2017/4/13.
*/
public class Account {
private long id;
private String name;
private Integer balance;
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 getBalance() {
return balance;
}
public void setBalance(Integer balance) {
this.balance = balance;
}
public Account(long id,String name,Integer balance){
super();
this.id=id;
this.name=name;
this.balance=balance;
}
public Account(String name,Integer balance){
super();
this.name=name;
this.balance=balance;
}
@Override
public String toString() {
return "[序号:"+id+",商品名称:"+name+",余额:"+balance;
}
}
AccountDao.class
package cn.edu.bzu.product.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.product.bean.Account;
/**
* Created by Administrator on 2017/4/13.
*/
public class AccountDao {
private MyHelper helper;
public AccountDao(Context context){
helper=new MyHelper(context);
}
public void insert(Account account){
SQLiteDatabase db=helper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("balance",account.getBalance());
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("balance",account.getBalance());
int count=db.update("account",values,"_id=?",new String[]{account.getId()+""});
db.close();
return count;
}
public List<Account> queryAll(){
SQLiteDatabase db=helper.getWritableDatabase();
Cursor c=db.query("account",null,null,null,null,null,"balance DESC");
List<Account> list=new ArrayList<Account>();
while (c.moveToNext()){
long id=c.getLong(c.getColumnIndex("_id"));
String name=c.getString(1);
int balance=c.getInt(2);
list.add(new Account(id,name,balance));
}
c.close();
db.close();
return list;
}
}
MyHelper.class
package cn.edu.bzu.product.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Administrator on 2017/4/13.
*/
public class MyHelper extends SQLiteOpenHelper {
public MyHelper(Context context){
super(context,"itCast.db",null,2);
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("onCreate");
db.execSQL("CREATE TABLE account(_id INTEGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("onUpgrade");
}
}
MainActivity.class
package cn.edu.bzu.product;
import android.content.DialogInterface;
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.product.bean.Account;
import cn.edu.bzu.product.dao.AccountDao;
public class MainActivity extends AppCompatActivity {
private List<Account> list;
private AccountDao dao;
private EditText nameET;
private EditText balanceET;
private MyAdapter adapter;
private ListView accountLV;
@Override
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);
balanceET=(EditText) findViewById(R.id.balanceET);
accountLV.setOnItemClickListener(new MyOnItemClickListener());
}
public void add(View v){
String name=nameET.getText().toString().trim();
String balance=balanceET.getText().toString().trim();
Account a=new Account(name,balance.equals("")?0:Integer.parseInt(balance));
dao.insert(a);
list.add(a);
adapter.notifyDataSetChanged();
accountLV.setSelection(accountLV.getCount()-1);
nameET.setText("");
balanceET.setText("");
Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
}
private class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
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 balanceTV=(TextView) item.findViewById(R.id.balanceTV);
final Account a=list.get(position);
idTV.setText(a.getId()+"");
nameTV.setText(a.getName());
balanceTV.setText(a.getBalance()+"");
ImageView upTV=(ImageView) item.findViewById(R.id.upTV);
ImageView downTV=(ImageView) item.findViewById(R.id.downTV);
ImageView deleteTV=(ImageView) item.findViewById(R.id.deleteTV);
upTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
a.setBalance(a.getBalance()+1);
notifyDataSetChanged();
dao.update(a);
}
});
downTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
a.setBalance(a.getBalance()-1);
notifyDataSetChanged();
dao.update(a);
}
});
deleteTV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
list.remove(a);
dao.delete(a.getId());
notifyDataSetChanged();
}
};
AlertDialog.Builder buileder =new AlertDialog.Builder(MainActivity.this);
buileder.setTitle("确定要删除吗?");
buileder.setPositiveButton("确定",listener);
buileder.setNegativeButton("取消",null);
buileder.show();
}
});
return item;
}
}
private class MyOnItemClickListener implements AdapterView.OnItemClickListener{
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Account a=(Account) adapterView.getItemAtPosition(i);
Toast.makeText(getApplicationContext(),a.toString(),Toast.LENGTH_SHORT).show();
}
}
}