商品展示

来源:互联网 发布:夜来听雨声,花落知多少 编辑:程序博客网 时间:2024/04/28 18:31

1.编写界面布局

<?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:orientation="vertical"
    tools:context="com.example.administrator.xzx.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <EditText
            android:id="@+id/et_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="商品名称"
            android:inputType="text"
            android:layout_weight="1"
            />
        <EditText
            android:id="@+id/et_number"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="商品数量"
            android:inputType="number"
            android:layout_weight="1"/>
        <ImageView
            android:id="@+id/et_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="add"
            android:src="@android:drawable/ic_input_add"/>
    </LinearLayout>

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

    </ListView>
</LinearLayout>

2.编写每一项的布局

<?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:text="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_id"
        android:textSize="20dp"
        android:layout_weight="1" />
    <TextView
        android:text="商品名称"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_name"
        android:layout_weight="1"
        android:textSize="20dp"/>

    <TextView
        android:text="商品数量"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_number"
        android:layout_weight="1"
        android:textSize="20dp"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/tv_up"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_up_float"/>
        <ImageView
            android:id="@+id/tv_down"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_down_float"/>

    </LinearLayout>
    <ImageView
        android:id="@+id/tv_delet"
        android:layout_width="25dp"
        android:layout_height="25dp"

        android:src="@android:drawable/ic_menu_delete"/>
</LinearLayout>

3.编写商品类 包含商品的id number和商品的名称

package com.example.administrator.xzx.entity;

/**
 * Created by Administrator on 2017/3/30.
 */

public class Goods {
    private long id;
    private String name;
    private int number;

    public Goods(long id, String name, int number) {
        this.id = id;
        this.name = name;
        this.number = number;
    }

    public Goods(String name, int number) {
        this.name = name;
        this.number = number;
    }

    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 getNumber() {
        return number;
    }

    public void setNumber(int number) {
        this.number = number;
    }

    @Override
    public String toString() {
        return "Goods{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", number=" + number +
                '}';
    }
}

4.创建适配器GoodsAdapter类

package com.example.administrator.xzx;
import android.content.Context;
import android.content.DialogInterface;
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.administrator.xzx.dao.GoodsDao;
import com.example.administrator.xzx.entity.Goods;

import java.util.List;

/**
 * Created by Administrator on 2017/3/30.
 */

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

 

    public GoodsAdapter(Context context, int resource, List<Goods> 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);
        View view=null;
        ViewHolder viewHolder;
        if(convertView==null){
            view= LayoutInflater.from(getContext()).inflate(R.layout.item,null);
            viewHolder=new ViewHolder();
            viewHolder.tvid=(TextView)view.findViewById(R.id.tv_id);
            viewHolder.tvname=(TextView)view.findViewById((R.id.tv_name));
            viewHolder.tvnumber=(TextView)view.findViewById(R.id.tv_number);
            viewHolder.ivup=(ImageView)view.findViewById(R.id.tv_up);
            viewHolder.ivdown=(ImageView)view.findViewById(R.id.tv_down);
            viewHolder.ivdelet=(ImageView)view.findViewById(R.id.tv_delet);
            view.setTag(viewHolder);
        }else{
            view=convertView;
            viewHolder=(ViewHolder)view.getTag();
        }
        viewHolder.tvid.setText(goods.getId()+"");
        viewHolder.tvname.setText(goods.getName());
        viewHolder.tvnumber.setText(goods.getNumber()+"");
        viewHolder.ivdelet.setOnClickListener(new View.OnClickListener() {
            @Override
            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 dialog, int which) {
                        goodsList.remove(goods);
                        goodsDao.delete(goods.getId());
                        notifyDataSetChanged();
                    }
                });
                builder.setNegativeButton("cancel",null);
                builder.show();
            }
        });
        viewHolder.ivup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goods.setNumber(goods.getNumber()-1);
                goodsDao.update(goods);
                notifyDataSetChanged();
            }
        });
        viewHolder.ivdown.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goods.setNumber(goods.getNumber()+1);
                goodsDao.update(goods);
                notifyDataSetChanged();
            }
        });
        return view;
    }

    class ViewHolder {
        TextView tvid;
        TextView tvname;
        TextView tvnumber;
        ImageView ivup;
        ImageView ivdown;
        ImageView ivdelet;

    }
}

5.进行数据库操作

创建JAVA类DBHelper

package com.example.administrator.xzx.db;


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

/**
 * Created by Administrator on 2017/3/30.
 */

public class DBHelper extends SQLiteOpenHelper {
    public static final String CREATE_GOODS = "create table goods(_id integer primary key autoincrement,name varchar(20),number 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) {

    }
}

6.创建goodsDao用作商品增删改查

package com.example.administrator.xzx.dao;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import com.example.administrator.xzx.db.DBHelper;
import com.example.administrator.xzx.entity.Goods;

import java.util.ArrayList;
import java.util.Deque;
import java.util.List;

/**
 * Created by Administrator on 2017/3/30.
 */

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("number",goods.getNumber());
        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("number",goods.getNumber());
        int count=sqLiteDatabase.update("goods",values,"_id=?",new String[]{goods.getId()+""});
        sqLiteDatabase.close();
        return count;
    }
    public List<Goods> queryAll(){
        List<Goods> goodsList =new ArrayList<>();
        SQLiteDatabase sqLiteDatabase=dbHelper.getReadableDatabase();
        Cursor cursor =sqLiteDatabase.query("goods",null,null,null,null,null,"number desc");
        while(cursor.moveToNext()){
            long id=cursor.getLong(cursor.getColumnIndex("_id"));
            String name=cursor.getString(cursor.getColumnIndex("name"));
            int number=cursor.getInt(cursor.getColumnIndex("number"));
            Goods goods=new Goods(id,name,number);
            goodsList.add(goods);
        }
        cursor.close();
        sqLiteDatabase.close();
        return goodsList;
    }
}

7.MainActivity 添加商品

package com.example.administrator.xzx;

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.administrator.xzx.dao.GoodsDao;
import com.example.administrator.xzx.entity.Goods;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private EditText etName;
    private EditText etNumber;
    private ListView lvGoods;
    private GoodsAdapter goodsAdapter;
    private GoodsDao goodsDao;
    private List<Goods> goodsList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etName = (EditText) findViewById(R.id.et_name);
        etNumber = (EditText) findViewById(R.id.et_number);
        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 add(View view) {
        String name = etName.getText().toString();
        String number = etNumber.getText().toString();
        if(TextUtils.isEmpty(name)||TextUtils.isEmpty(number))
            return;
        Goods goods = new Goods(name, number.equals("") ? 0 : Integer.parseInt(number));
        goodsDao.add(goods);
        goodsList.add(goods);
        goodsAdapter.notifyDataSetChanged();
        etName.setText("");
        etNumber.setText("");
        Toast.makeText(this,"添加成功",Toast.LENGTH_LONG).show();

    }
}

8.运行测试结果

0 0