一.创建程序
首先创建一个名为“商品展示”的应用程序,设计用户交互界面,如图所示
“商品展示”程序的代码如下所示:
<?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"
android:orientation="vertical"
tools:context="bzu.edhu.cn.product.MainActivity">
<LinearLayout
android:id="@+id/addLL"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/et1"
android:hint="商品名称"
android:inputType="textPersonName"
/>
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/et2"
android:hint="金额"
android:inputType="number"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="add"
android:id="@+id/addIV"
android:src="@android:drawable/ic_input_add"
/>
</LinearLayout>
<ListView
android:id="@+id/accountLV"
android:layout_below="@id/addLL"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</ListView
>
</LinearLayout>
二.创建ListView Item布局
由于本例用到了ListView布局,因此需要编写一个Listview item的布局。在res/layout目录下创建一个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="match_parent"
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: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:id="@+id/ItemLL"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/upIV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:src="@android:drawable/arrow_up_float"
/>
<ImageView
android:id="@+id/downIV"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginBottom="2dp"
android:src="@android:drawable/arrow_down_float"
/>
</LinearLayout>
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:id="@+id/deleteIV"
android:src="@android:drawable/ic_menu_delete"
/>
</LinearLayout>
三.创建数据库
由于数据库属于数据操作,因此需要在第一个包下创建一个名为dao的包。并在该包下定义一个MyHelper类继承自SQLiteOpenHelper,创建数据库代码如下:
package bzu.edhu.cn.product.dao;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.Settings;
/**
* Created by Administrator on 2017/4/17.
*/
public class MyHelper extends SQLiteOpenHelper {
private static final String create_account="create table account(id integer primary key autoincrement ,name varchar(20),balance integer)";
public MyHelper(Context context){
super(context,"product.db",null,2);
}
@Override
public void onCreate(SQLiteDatabase db) {
System.out.println("ONcreate");
db.execSQL(create_account);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
四.创建Account类
在操作数据库时将数据存放在一个javabean 对象中操作起来会比较方便。因此需要在第一个包中创建一个存放JavaBean类,即Account类
代码如下:
package bzu.edhu.cn.product;
/**
* Created by Administrator on 2017/4/17.
*/
public class Account {
private Long id;
private String name;
private Integer balance;
public Account(Long id,String name,Integer balance){
super();
this.id=id;
this.name=name;
this.balance=balance;
}
public Account(String name, int i){
super();
}
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;
}
@Override
public String toString() {
return "[序号:"+id+",商品名称:"+name+",余额:"+balance+"]";
}
}
五.创建数据操作逻辑类
代码如下:
package bzu.edhu.cn.product.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import java.util.ArrayList;
import java.util.List;
import bzu.edhu.cn.product.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 sqLiteDatabase=myHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("balance",account.getBalance());
Long id=sqLiteDatabase.insert("account",null,values);
account.setId(id);
sqLiteDatabase.close();
}
public int delete(Long id){
SQLiteDatabase sqLiteDatabase=myHelper.getWritableDatabase();
int count=sqLiteDatabase.delete("account","id=?",new String [] {id+""});
sqLiteDatabase.close();
return count;
}
public int update(Account account){
SQLiteDatabase sqLiteDatabase=myHelper.getWritableDatabase();
ContentValues values=new ContentValues();
values.put("name",account.getName());
values.put("balance",account.getBalance());
int count=sqLiteDatabase.update("account",values,"id=?",new String[]{account.getId()+""});
sqLiteDatabase.close();
return count;
}
public List<Account> queryAll(){
SQLiteDatabase db=myHelper.getReadableDatabase();
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;
}
}
}
六.编写界面交互代码(MainActivity)
代码如下:
package bzu.edhu.cn.product;
import android.app.Activity;
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.Adapter;
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 bzu.edhu.cn.product.dao.AccountDao;
public class MainActivity extends Activity {
//需要适配的数据集合
private List<Account> list;
//数据库增删改查操作类
private AccountDao dao;
private EditText et1;
private EditText et2;
//适配器
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);
et1=(EditText)findViewById(R.id.et1);
et2=(EditText)findViewById(R.id.et2);
//添加监听器,监听条目点击事件
accountLV.setOnItemClickListener(new MyOnItemClickListener());
}
public void add(View v){
String name=et1.getText().toString().trim();
String balance=et2.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);
et1.setText("");
et2.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) {
//重用convertView
View item=convertView!=null?convertView:View.inflate(getApplicationContext(),R.layout.item,null);
//获取该视图中的TextView
TextView idTV=(TextView) item.findViewById(R.id.idTV);
TextView nameTV=(TextView) item.findViewById(R.id.et1);
TextView balanceTV=(TextView)item.findViewById(R.id.et2);
final Account a=list.get(position);
//把Account对象中的数据放到TextView中
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() {
@Override
public void onClick(View v) {
a.setBalance(a.getBalance()+1);//修改值
notifyDataSetChanged();//刷新界面
dao.update(a);//更新数据库
}
});
//向下箭头的点击事件触发的方法
downIV.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.setBalance(a.getBalance()-1);
notifyDataSetChanged();
dao.update(a);
}
});
//删除图片的点击事件触发的方法
deleteIV.setOnClickListener(new View.OnClickListener() {
@Override
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();
}
};
//创建对话框
AlertDialog.Builder builder=new AlertDialog.Builder(MainActivity.this);
builder.setTitle("你确定要删除吗?");
//设置确定按钮的文本以及监听器
builder.setPositiveButton("确定",listener);
builder.setNegativeButton("取消",null);
builder.show();
}
});
return item;
}
}
//ListView 的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();
}
}
}
如此便可以实现商品展示了哦!!!