商品展示案例

总布局界面

    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"
    tools:context="com.example.bz0209.myapplication.MainActivity">

            android:layout_width="match_parent"
        android:layout_height="wrap_content">

                    android:id="@+id/etName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="商品名称"
            android:inputType="text" />

                    android:id="@+id/etNumber"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="商品数量"
            android:inputType="number" />

                    android:id="@+id/ivAdd"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="addGoods"
            android:src="@android:drawable/ic_input_add" />
   


            android:id="@+id/lvGoods"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

***********************************************************
分布局界面

    android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:id="@+id/tvId"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="1"
        android:textSize="20sp" />

            android:id="@+id/tvName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="商品名称"
        android:textSize="20sp" />

            android:id="@+id/tvAmount"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="商品数量"
        android:textSize="20sp" />

            android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">

                    android:id="@+id/ivUp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_up_float" />

                    android:id="@+id/ivDown"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_down_float" />
   


            android:id="@+id/ivDelete"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@android:drawable/ic_menu_delete" />

**************************************************************
创建数据库
package com.example.bz0209.myapplication.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 {
    private static final String CREATE_GOODS="create table goods(_id integer primary 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 sqLiteDatabase, int i, int i1) {

    }
}
*******************************************************************
商品类
package com.example.bz0209.shop.entity;

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

public class Goods {
    private long id;
    private String name;
    private int amount;
    public Goods(long id,String name,int amount){
        super();
        this.id=id;
        this.name=name;
        this.amount=amount;
    }
    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 int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }
    public Goods(String name,int amount){
        super();
        this.name=name;
        this.amount=amount;
    }
    public Goods(){
        super();
    }
    public String toString(){
        return "[序号:"+id+",商品名称:"+name+",余额:"+amount+"]";
    }
}
************************************************************
创建数据库库逻辑类
package com.example.bz0209.myapplication.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.example.bz0209.myapplication.db.DBHelper;
import com.example.bz0209.myapplication.entity.Goods;

import java.sql.SQLData;
import java.util.ArrayList;
import java.util.List;

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

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());
        long id=sqLiteDatabase.insert("goods",null,values);
        goods.setId(id);
        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(){
        List goodsList=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(id,name,amount);
            goodsList.add(goods);
        }
        cursor.close();
        sqLiteDatabase.close();
        return goodsList;
    }
}

******************************************************************************************
交互界面
package com.example.bz0209.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

import com.example.bz0209.myapplication.dao.GoodsDao;
import com.example.bz0209.myapplication.entity.Goods;
import com.example.bz0209.myapplication.entity.GoodsAdapter;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etAmount;
    private GoodsDao goodsDao;
    private ListView lvGoods;
    private GoodsAdapter goodsAdapter;
    private List goodsList;
    @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,goodsDao);
        lvGoods.setAdapter(goodsAdapter);
    }
    public void addGoods(View view){
        String name=etName.getText().toString();
        String amount=etAmount.getText().toString();
        if(TextUtils.isEmpty(name)||TextUtils.isEmpty(amount))
            return;
        Goods goods=new Goods(name,Integer.parseInt(amount));
        goodsDao.add(goods);
        goodsList.add(goods);
        goodsAdapter.notifyDataSetChanged();
        etName.setText("");
        etAmount.setText("");
        Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();
    }
}
*********************************************************************************
适配器
package com.example.bz0209.myapplication.entity;

import android.content.Context;
import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.support.annotation.NonNull;
import android.support.v7.app.AlertDialog;
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.myapplication.R;
import com.example.bz0209.myapplication.dao.GoodsDao;

import java.util.List;

/**
 * Created by Administrator on 2017/5/5.
 */

public class GoodsAdapter extends ArrayAdapter {
    private int resounceId;
    private GoodsDao goodsDao;
    private List goodsList;

    public GoodsAdapter(Context context, int resource, List objects,GoodsDao goodsDao) {
        super(context, resource, objects);
        resounceId = resource;
        goodsList=objects;
        this.goodsDao=goodsDao;
    }

    @NonNull
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final Goods goods = getItem(position);
        ViewHolder viewHolder;
        View view =null;
        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.tvAmount);
            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()+"");
        viewHolder.ivDelete.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v){
                AlertDialog.Builder builder=new AlertDialog.Builder(getContext());
                builder.setTitle("你确定要删除吗?");
                builder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        goodsDao.delete(goods.getId());
                        goodsList.remove(goods);
                        notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("cancel",null);
                builder.show();
            }
        });
        viewHolder.ivUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goods.setAmount(goods.getAmount()-1);
                goodsDao.update(goods);
                notifyDataSetChanged();
            }
        });
        viewHolder.ivDown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goods.setAmount(goods.getAmount()+1);
                goodsDao.update(goods);
                notifyDataSetChanged();
            }
        });
        return view;
    }
    class ViewHolder{
        TextView tvId;
        TextView tvName;
        TextView tvAmount;
        ImageView ivUp;
        ImageView ivDown;
        ImageView ivDelete;
    }
}

商品展示案例_第1张图片 商品展示案例_第2张图片

你可能感兴趣的:(商品展示案例)