首先,创造一个应用程序,布局文件(activity_main.xml),如下图所示:
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="bzu.edu.cn.shop.MainActivity"
>
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
android:hint="商品名称"
android:id="@+id/nameef"
android:layout_width="0dp"
android:layout_weight="1"
android:inputType="textPersonName"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/balanceef"
android:hint="金额"
android:inputType="number"
/>
android:layout_height="wrap_content"
android:id="@+id/addiv"
android:onClick="add"
android:src="@android:drawable/ic_input_add"/>
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/addLL">
创建ListView Item布局,在res/layout目录下创建一个item.xml文件,代码如图所示:
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="10dp">
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="13"
android:textSize="20sp"
android:textColor="#000000"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:text="PQ"
android:textSize="20sp"
android:textColor="#000000"/>
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:singleLine="true"
android:text="12345"
android:textSize="20sp"
android:textColor="#000000"/>
android:layout_height="wrap_content"
android:orientation="vertical">
android:layout_marginBottom="2sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_up_float"/>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@android:drawable/arrow_down_float"/>
android:layout_marginBottom="2sp"
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@android:drawable/ic_menu_delete"/>
创建数据库,在包下创建一个名为dao的包,并在该包下定义一个MyHelper类继承自SQLiteOpenHelper,创建数据库代码如下:
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Administrator on 2017/4/17.
*/
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 INTERGER PRIMARY KEY AUTOINCREMENT,name VARCHAR(20),balance INTERGER)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
System.out.println("onUpgrade");
}
创建Acount类:
在包下创建一个bean的包用于存放JavaBean的类,然后在包下定义一个Account,代码如下:
package bzu.edu.cn.shop.bean;
/**
* Created by Administrator on 2017/4/17.
*/
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.name=name;
this.balance=balance;
}
public Account(String name, int i){
super();
}
public String toString(){
return "[序号:"+id+",商品名称:"+name+",余额:"+balance+"]";
}
}
创建数据操作逻辑类
在dao下创建AccountDao类用于操作数据,具体代码如下:
package bzu.edu.cn.shop.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import bzu.edu.cn.shop.bean.Account;
/**
* Created by Administrator on 2017/4/17.
*/
public class AccountDao {
private MYHelper myHelper;
public AccountDao(Context context){
myHelper=new MYHelper(context);
}
public void insert (Account account){
SQLiteDatabase db=myHelper.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=myHelper.getWritableDatabase();
int count=db.delete("account","id=?",new String[] {id+""});
db.close();
return count;
}
public int update (Account account){
SQLiteDatabase db=myHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("balance",account.getBalance());
int count=db.update("acount",values,"id=?",new String[]{account.getId()+""});
db.close();
return count;
}
public List
SQLiteDatabase db=myHelper.getWritableDatabase();
Cursor c=db.query("acount",null,null,null,null,null,"balance DESC");
List
while (c.moveToFirst()){
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;
}
}
编辑界面交互代码(MainActivity),具体代码如下:
import java.util.List;
import java.util.Objects;
import bzu.edu.cn.shop.dao.AccountDao;
import bzu.edu.cn.shop.bean.Account;
public class MainActivity extends Activity {
private List
private AccountDao dao;
private EditText nameef;
private EditText balanceef;
private Mydapter 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 Mydapter();
accountlv.setAdapter(adapter);
}
private void initView(){
accountlv=(ListView) findViewById(R.id.accountlv);
nameef=(EditText)findViewById(R.id.nameef);
balanceef=(EditText)findViewById(R.id.balanceef);
accountlv.setOnItemLongClickListener(new MyOnItemClickListener());
}
public void add(View view){
String name=nameef.getText().toString().trim();
String balance=balanceef.getText().toString().trim();
Account a=new Account(name,balance.equals("")?0:Integer.parseInt(balance));
list.add(a);
adapter.notifyDataSetChanged();
accountlv.SetSelection(accountlv.getCount()-1);
nameef.setText("");
balanceef.setText("");
}
private class Mydapter extends BaseAdapter{
public int getCount(){
return list.size();
}
public Object getItem(int postion){
return list.get(postion);
}
public long getItemId(int postion){
return postion;
}
@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 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() {
public void onClick(View v) {
a.setBalance(a.getBalance()-1);
notifyDataSetChanged();
dao.update(a);
}
});
downiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
a.setBalance(a.getBalance()-1);
notifyDataSetChanged();;
dao.update(a);
}
});
deleteiv.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
android.content.DialogInterface.OnClickListener listener=new android.content.DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which) {
list.remove(a);
dao.delete(a.getId());
notifyDataSetChanged();
}
};
android.app.AlertDialog.Builder builder=new android.app.AlertDialog.Builder(MainActivity.this);
builder.setTitle("确定要删除么?");
builder.setPositiveButton("确定",listener);
builder.setPositiveButton("取消",null);
builder.show();
}
});
return item;
}
}
private abstract class MyOnItemClickListener implements DialogInterface.OnClickListener {
public void OnClickListener(AdapterView>parent,View view,int postion,long id){
Account a=(Account) parent.getItemAtPosition(postion);
Toast.makeText(getApplicationContext(),a.toString(),Toast.LENGTH_SHORT).show();
}
}
}
运行结果如下: