商品展示
来源:互联网 发布:树洞源码 编辑:程序博客网 时间:2024/04/27 22:14
1、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: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="bzu.edu.cn.productlist.MainActivity" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:hint="商品名称" android:id="@+id/EdName" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:hint="金额" android:id="@+id/EdBalance" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:drawable/ic_input_add" android:id="@+id/IVAdd" android:onClick="addProduct"/> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" > <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/lv_product"> </ListView> </LinearLayout></LinearLayout>
2、创建ListView Item布局
在res/layout目录下创建product_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"> <TextView android:layout_width="0dp" android:layout_height="30dp" android:id="@+id/idTV" android:text="1" android:gravity="center" android:layout_weight="1" android:layout_marginLeft="5dp" android:textSize="20sp"/> <TextView android:layout_width="0dp" android:layout_height="30dp" android:id="@+id/nameTV" android:text="phone" android:gravity="center" android:layout_weight="1" android:layout_marginLeft="5dp" android:textSize="20sp"/> <TextView android:layout_width="0dp" android:layout_height="30dp" android:id="@+id/balanceTV" android:text="7777" android:gravity="center" android:layout_weight="1" android:layout_marginLeft="5dp" android:textSize="20sp"/> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:drawable/arrow_up_float" android:layout_marginBottom="5dp" android:id="@+id/upIV" android:onClick="updateProductUp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:drawable/arrow_down_float" android:id="@+id/downIV" android:onClick="updateProductDown" /> </LinearLayout> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:drawable/ic_menu_delete" android:layout_marginLeft="5dp" android:id="@+id/deleteIV" android:onClick="deleteProduct"/></LinearLayout>
3、创建数据库
创建 DataBaseHelper类 继承 SQLiteOpenHelper
package bzu.edu.cn.productlist.ProductHelper;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteOpenHelper;import android.util.Log;/** * Created by Administrator on 2017/4/17. */public class DataBaseHelper extends SQLiteOpenHelper { private final String CREATE_ACCOUNT="create table account(id integer primary key autoincrement,name text,balance integer)"; public DataBaseHelper(Context context) { super(context, "product.db", null, 2); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(CREATE_ACCOUNT); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log.d("DataBaseHelper","onUpgrade"); }}
4、创建Product类
package bzu.edu.cn.productlist.Product;/** * Created by Administrator on 2017/4/17. */public class ProductBean { private int id; private String name; private Integer banlance; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getBanlance() { return banlance; } public void setBanlance(Integer banlance) { this.banlance = banlance; } public ProductBean(Integer id, String name, Integer banlance) { this.id = id; this.name = name; this.banlance = banlance; } public ProductBean() { super(); } public String toString(){ return "[序号:"+id+",商品名称:"+name+",余额:,"+banlance+"]"; }}
5、创建数据库操作类ProductDao
package bzu.edu.cn.productlist.ProductHelper;import android.accounts.Account;import android.content.ContentValues;import android.content.Context;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;import android.widget.Toast;import java.util.ArrayList;import java.util.List;import bzu.edu.cn.productlist.Product.ProductBean;/** * Created by Administrator on 2017/4/17. */public class ProductDao { private Context context; private DataBaseHelper dataBaseHelper; public ProductDao(Context context) { this.context=context; dataBaseHelper=new DataBaseHelper(this.context); } public void addData(ProductBean productBean){//添加数据 SQLiteDatabase db=dataBaseHelper.getWritableDatabase(); ContentValues contentValues=new ContentValues();//用来装载要插入的数据的Map contentValues.put("name",productBean.getName()); contentValues.put("balance",productBean.getBanlance()); long id=db.insert("account",null,contentValues); productBean.setId((int)id);//得到id db.close();//关闭数据库 } public List<ProductBean> queryData(){ List<ProductBean> productList=new ArrayList<ProductBean>(); SQLiteDatabase db=dataBaseHelper.getWritableDatabase(); Cursor cursor = db.query("account",null,null,null,null,null,null); while(cursor.moveToNext()) {//将查询出来的数据添加到集合中 ProductBean productBean = new ProductBean(); productBean.setId(cursor.getInt(cursor.getColumnIndex("id"))); productBean.setName(cursor.getString(cursor.getColumnIndex("name"))); productBean.setBanlance(cursor.getInt(cursor.getColumnIndex("balance"))); productList.add(productBean); } db.close(); return productList; } public void updateData(ProductBean productBean){ SQLiteDatabase db=dataBaseHelper.getWritableDatabase(); ContentValues contentValues=new ContentValues(); contentValues.put("name",productBean.getName()); contentValues.put("balance",productBean.getBanlance()); db.update("account",contentValues,"id=?",new String[]{productBean.getId()+""});//根据id修改 db.close(); } public void deletedata(ProductBean productBean){ SQLiteDatabase db=dataBaseHelper.getWritableDatabase(); db.delete("account","id=?",new String[]{productBean.getId()+""});//根据id删除 db.close(); }}
6、自定义适配器ProductAdepter类
package bzu.edu.cn.productlist.ProductList;import android.app.AlertDialog;import android.content.Context;import android.content.DialogInterface;import android.support.annotation.NonNullimport android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView;import java.util.List;import bzu.edu.cn.productlist.Product.ProductBean;import bzu.edu.cn.productlist.ProductHelper.ProductDao;import bzu.edu.cn.productlist.R;/** * Created by Administrator on 2017/4/18. */public class ProductAdapter extends BaseAdapter { private Context context; private List<ProductBean> list; private ProductDao productDao; public ProductAdapter(Context context,List<ProductBean> list) {//构造方法 this.context = context; this.list=list; } @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;//根据位置获取id } @NonNull @Override public View getView(int position, View convertView, ViewGroup parent) { productDao=new ProductDao(context);//重用convertView View view=convertView!=null?convertView:View.inflate(context,R.layout.product_item,null);//获取该视图的TextView TextView IdTV = (TextView) view.findViewById(R.id.idTV); TextView nameTV = (TextView) view.findViewById(R.id.nameTV); TextView balanceTV = (TextView) view.findViewById(R.id.balanceTV);//获取当前位置的Product对象 final ProductBean pro=list.get(position);//为TextView赋值 IdTV.setText(pro.getId()+""); nameTV.setText(pro.getName()); balanceTV.setText(pro.getBanlance()+""); //获取ImageView控件 ImageView upIV= (ImageView) view.findViewById(R.id.upIV); ImageView downIV= (ImageView) view.findViewById(R.id.downIV); ImageView deleteIV= (ImageView) view.findViewById(R.id.deleteIV); //向上箭头点击事件触发的方法 upIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pro.setBanlance(pro.getBanlance()+1);//修改值 productDao.updateData(pro);//更新数据库 notifyDataSetChanged();//刷新界面 } }); //向下箭头点击事件触发的方法 downIV.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { pro.setBanlance(pro.getBanlance()-1); productDao.updateData(pro); notifyDataSetChanged(); } }); //删除图标点击事件触发的方法 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(pro);//从集合中移除 productDao.deletedata(pro);//从数据库中删除 notifyDataSetChanged();//刷新界面 } }; //创建对话框 AlertDialog.Builder builder=new AlertDialog.Builder(context); builder.setTitle("确定要删除吗?"); builder.setPositiveButton("确定",listener); builder.setNegativeButton("取消",null); builder.show(); } }); return view; }}7、界面交互代码MainActivitypackage bzu.edu.cn.productlist;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.AdapterView;import android.widget.EditText;import android.widget.ImageView;import android.widget.ListView;import android.widget.Toast;import java.util.List;import bzu.edu.cn.productlist.Product.ProductBean;import bzu.edu.cn.productlist.ProductHelper.ProductDao;import bzu.edu.cn.productlist.ProductList.ProductAdapter;public class MainActivity extends AppCompatActivity { private EditText EdName; private EditText EdBalance; private ImageView IvAdd; private ListView listView; private ProductDao productDao; private List<ProductBean> list; private ProductAdapter productAdapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);//初始化控件 EdName= (EditText) findViewById(R.id.EdName); EdBalance=(EditText)findViewById(R.id.EdBalance); IvAdd=(ImageView)findViewById(R.id.IVAdd); listView= (ListView) findViewById(R.id.lv_product);//创建ProductDao对象 productDao=new ProductDao(this);//查询所有记录 list=productDao.queryData(); //创建适配器 productAdapter=new ProductAdapter(this,list); listView.setAdapter(productAdapter);//添加监听器,监听条目点击事件 listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ProductBean productBean= (ProductBean) parent.getItemAtPosition(position); Toast.makeText(MainActivity.this,productBean.toString(),Toast.LENGTH_LONG).show(); } }); } //点击加号所触发的事件 public void addProduct(View view){ String pname=EdName.getText().toString(); String pbalance=EdBalance.getText().toString(); if(pname.equals("")&&pbalance.equals("")){//判断是否为空,为空弹出Toast Toast.makeText(this,"商品名称和金额不能为空",Toast.LENGTH_LONG).show(); }else{ ProductBean productBean=new ProductBean(); productBean.setName(pname); productBean.setBanlance(Integer.parseInt((pbalance)));//类型转换 productDao.addData(productBean);//插入数据库 list.add(productBean);//插入集合 } productAdapter.notifyDataSetChanged();//刷新界面 listView.setSelection(listView.getCount()-1);//选中最后一个 EdName.setText(""); EdBalance.setText(""); }}运行结果图
0 0
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- 商品展示
- Androd系统开发最佳学习实践
- 2017Google Codejam round1b Problem B. Stable Neigh-bors
- React Native打包代码解析与拆分Bundle做法
- 并查集
- 请求无效
- 商品展示
- CentOS下tar解压 gz解压 bz2等各种解压文件使用方法
- ValueError: need more than 1 value to unpack
- jQuery取消后续事件的执行
- 算法训练 邮票
- 15. Struts2_值栈
- pwm控制蜂鸣器驱动移植及初步认识Kconfig
- android message发送问题总结
- [编程题] 组装三角形