Android实现商品展示效果

一、 创建手机界面布局

创建一个activity_main.xml文件代码如下:



 

 
 
 
 
 

 


效果图如下:

Android实现商品展示效果_第1张图片

二、创建一个存储展示的xml文件

创建一个名为item.xml的布局文件,代码如下:



 
 
 
 
 
 


三、创建数据库

在com.example.bz0209.shopshowdemo 的包下创建一个名为db的包,并在该包下定义一个dbHelper类继承自SQLiteOpenHelper,创建数据库代码如下:

package com.example.bz0209.shopshowdemo.db;

import android.content.Context;

import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Created by Administrator on 2017/4/28.
 */

public class DBHelper extends SQLiteOpenHelper{
 public static final String CREATE_GOODS="create table goods(_id integer primary key autoincrement,name varchar(20),amount integer)";
 public DBHelper(Context context,int version){
 super(context,"goods.db",null,version);
 }
 @Override
 public void onCreate(SQLiteDatabase db) {
 db.execSQL(CREATE_GOODS);
 }

 @Override
 public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion) {
 }
}

四、创建GoodsAdapter类

为了操作数据库方便,创建一个GoodsAdapter类。因此需要在com.example.bz0209.shopshowdemo 的包下创建一个名为entity的包,然后在com.example.bz0209.shopshowdemo.entityd的包下定义一个GoodsAdapter类,代码如下:

package com.example.bz0209.shopshowdemo;

import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.bz0209.shopshowdemo.entity.Goods;

import java.util.List;

/**
 * Created by Administrator on 2017/4/27.
 */

public class GoodsAdapter extends ArrayAdapter{
 private int resounceId;

 public GoodsAdapter(Context context, int resource, List objects) {
 super(context, resource, objects);
 resounceId=resource;
 }

 @NonNull
 @Override
 public View getView(int position, View convertView, ViewGroup parent) {
 Goods goods=getItem(position);
 View view=null;
 ViewHolder viewHolder;
 if (convertView==null){
  view = LayoutInflater.from(getContext()).inflate(resounceId,null);
  viewHolder =new ViewHolder();
  viewHolder.tvId=(TextView) view.findViewById(R.id.tvId);
  viewHolder.tvName=(TextView) view.findViewById(R.id.tvName);
  viewHolder.tvAmount=(TextView) view.findViewById(R.id.etAmount);
  viewHolder.ivUp=(ImageView) view.findViewById(R.id.ivUP);
  viewHolder.ivDown=(ImageView) view.findViewById(R.id.ivDown);
  viewHolder.ivDelete=(ImageView) view.findViewById(R.id.ivDelete);
  view.setTag(viewHolder);
 }else {
  view=convertView;
  viewHolder=(ViewHolder) view.getTag();
 }
 viewHolder.tvId.setText(goods.getId()+"");
 viewHolder.tvName.setText(goods.getName());
 viewHolder.tvAmount.setText(goods.getAmount()+"");

 return view;
 }
 class ViewHolder{
 TextView tvId;
 TextView tvName;
 TextView tvAmount;
 ImageView ivUp;
 ImageView ivDown;
 ImageView ivDelete;
 }
}

五、创建数据操作逻辑类

在com.example.bz0209.shopshowdemo 的包下创建一个名为dao的包,并创建一个名为GoodsDao的类,用于操作数据。代码如下:

public class GoodsDao {
 private DBHelper dbHelper;


 public GoodsDao(Context context){
 dbHelper=new DBHelper(context,1);
 }

 public void add(Goods goods){
 SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
 ContentValues values=new ContentValues();
 values.put("name",goods.getName());
 values.put("amount",goods.getAmount());
 sqLiteDatabase.insert("goods",null,values);
 sqLiteDatabase.close();


 }
 public int delete(long id){
 SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
 int count=sqLiteDatabase.delete("goods","id=?",new String[]{id+""});
 sqLiteDatabase.close();
 return count;

 }
 public int update(Goods goods){
 SQLiteDatabase sqLiteDatabase=dbHelper.getWritableDatabase();
 ContentValues values=new ContentValues();
 values.put("name",goods.getName());
 values.put("amount",goods.getAmount());
 int count=sqLiteDatabase.update("goods",values,"id=?",new String[]{goods.getId()+""});
 sqLiteDatabase.close();
 return count;
 }
 public List queryAll(){
 ListgoodsList=new ArrayList<>();
 SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
 Cursor cursor=sqLiteDatabase.query("goods",null,null,null,null,null,"amount desc");
 while (cursor.moveToNext()){
  long id=cursor.getLong(cursor.getColumnIndex("_id"));
  String name=cursor.getString(cursor.getColumnIndex("name"));
  int amount=cursor.getInt(cursor.getColumnIndex("amount"));
  Goods goods=new Goods(name,amount);
  goodsList.add(goods);
 }
 cursor.close();
 sqLiteDatabase.close();
 return goodsList;
 }

}

六、编写界面交互代码

数据库操作完成后,需要界面与数据库进行交互,用于实现将数据库中的数据以ListView的形式展现在界面上具体代码如下:

package com.example.bz0209.shopshowdemo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import com.example.bz0209.shopshowdemo.dao.GoodsDao;
import com.example.bz0209.shopshowdemo.entity.Goods;
import java.util.List;
public class activity_main extends AppCompatActivity {
 private EditText etName;
 private EditText etAmount;
 private ListView lvGoods;
 private GoodsAdapter goodsAdapter;
 private GoodsDao goodsDao;
 private ListgoodsList;
 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 etName=(EditText)findViewById(R.id.etName);
 etAmount=(EditText)findViewById(R.id.etAmount);
 lvGoods=(ListView)findViewById(R.id.lvGoods);
 goodsDao=new GoodsDao(this);
 goodsList=goodsDao.queryAll();
 goodsAdapter=new GoodsAdapter(this,R.layout.item,goodsList);
 lvGoods.setAdapter(goodsAdapter);
 }
 public void addGoods(View view){
 String name=etName.getText().toString();
 String amount=etAmount.getText().toString();
 Goods goods=new Goods(name,amount.equals("")?0:Integer.parseInt(amount));
 goodsDao.add(goods);

 }
}

七、完成后的结果展示

(1)创建起数据库的图为:

Android实现商品展示效果_第2张图片

Android实现商品展示效果_第3张图片

(2)最后的运行结果图:

Android实现商品展示效果_第4张图片

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

你可能感兴趣的:(Android实现商品展示效果)