简单商品展示案例(ListView)

本文介绍了如何创建一个简单的Android商品展示界面,通过ListView显示商品。文章包含关键代码片段,强调了界面设计、控件ID命名的重要性及代码组织结构。商品实体类、数据库操作和商品操作类的使用使得数据管理更加便捷。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天做的是商品展示界面,先上一张运行效果图:
这里写图片描述
额,这是要求做到的,下面才是实际的
这里写图片描述
这里写图片描述

接下来是代码。

界面代码:
activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="cn.edu.bzu.test02.MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <EditText
            android:id="@+id/nameET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="商品名称"/>
        <EditText
            android:id="@+id/balanceET"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:hint="金额"/>
        <ImageView
            android:id="@+id/add"
            android:layout_width="wrap_content"
            android:layout_height="25dp"
            android:layout_weight="1"
            android:onClick="add"
            android:src="@android:drawable/ic_input_add"/>
    </LinearLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="30dp">

    </ListView>

</RelativeLayout>

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="wrap_content"
    android:orientation="horizontal">

    <TextView
        android:id="@+id/idTV"
        android:layout_width="wrap_content"
        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="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:text="PQ"
        android:textColor="#000000"/>
    <TextView
        android:id="@+id/balanceTV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="12345"
        android:textColor="#000000"/>
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <ImageView
            android:id="@+id/upIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_up_float"/>
        <ImageView
            android:id="@+id/downIV"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/arrow_down_float"/>
    </LinearLayout>
    <ImageView
        android:id="@+id/deleteIV"
        android:layout_width="25dp"
        android:layout_height="25dp"
        android:src="@android:drawable/ic_menu_delete"/>

</LinearLayout>

颜色什么的自己调,喜欢什么用什么,可以在颜色中输入“#000”然后点前面的小黑方框就可以选择自己喜欢的颜色了。
还有非常重要的的是每个控件的id属性要简单易懂,不要很随便,否则就后悔吧,控件多了根本分不清。
控件的id属性要简单易懂,不要很随便!
控件的id属性要简单易懂,不要很随便!
控件的id属性要简单易懂,不要很随便!
重要的事情说三遍。。。。。。

接下来是各个类,额,类最好分好包,显得整齐,也很容易理解,包名也要简单易懂。。。

商品实体类(Goods):

package cn.edu.bzu.store.bean;

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

public class Goods {
    private long id;
    private String name;
    private Integer balance;

    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;
    }

    public Goods(Long id, String name, Integer balance) {
        super();
        this.id=id;
        this.balance = balance;
        this.name = name;
    }
    public Goods(String name,Integer balance){
        super();
        this.name=name;
        this.balance=balance;
    }
    public Goods(){
        super();
    }

    public String toString() {
        return "名称" + name + "价格" + balance;
    }
}

数据库操作(DBHelper):

package cn.edu.bzu.store.db;

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

public class DBHelper extends SQLiteOpenHelper {
    private Context context;
    public static final String DB_NAME = "GoodsStore.db";
    public static final String CREATE_GOODS = "create table goods(id integer primary key autoincrement,name varchar(20),balance integer)";

    public DBHelper(Context context) {
        super(context, DB_NAME, null, 2);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        System.out.println("create succeed");
        db.execSQL(CREATE_GOODS);
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
        System.out.println("onUpgrade succeed");
    }

}

商品操作类(GoodsDao):

package cn.edu.bzu.store.dao;

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

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

import cn.edu.bzu.test02.bean.Goods;
import cn.edu.bzu.test02.db.DBHelper;

public class GoodsDao {
    private DBHelper helper;

    public GoodsDao(Context context) {
        helper = new DBHelper(context);
    }

    public void insert(Goods goods) {
        SQLiteDatabase db = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", goods.getName());
        values.put("balance", goods.getBalance());
        long id = db.insert("goods", null, values);
        goods.setId(id);
        db.close();
    }

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

    public int update(Goods goods) {
        SQLiteDatabase DB = helper.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", goods.getName());
        values.put("balance", goods.getBalance());
        int count = DB.update("goods", values, "id=?", new String[]{goods.getId() + ""});
        DB.close();
        return count;
    }

    public List<Goods> queryAll() {
        SQLiteDatabase db = helper.getReadableDatabase();
        Cursor curor = db.query("goods", null, null, null, null, null, "balance DESC");
        List<Goods> list = new ArrayList<Goods>();
        while (curor.moveToNext()) {
            long id = curor.getLong(curor.getColumnIndex("id"));
            String name = curor.getString(1);
            int balance = curor.getInt(2);
            list.add(new Goods(id, name, balance));
        }
        db.close();
        curor.close();
        return list;
    }
}

MainAvtivity:

package cn.edu.bzu.store;

import android.app.Activity;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ArrayAdapter;
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 cn.edu.bzu.test02.bean.Goods;
import cn.edu.bzu.test02.dao.GoodsDao;

public class MainActivity extends Activity {
    private ListView goodsLV;
    private GoodsDao goodsdao;
    private EditText nameET;
    private EditText balanceET;
    private MyAdapter adapter;
    private List<Goods> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        goodsdao = new GoodsDao(this);
        list = goodsdao.queryAll();
        adapter = new MyAdapter();
        goodsLV.setAdapter(adapter);
    }

    private void initView() {
        goodsLV = (ListView) findViewById(R.id.listView);
        nameET = (EditText) findViewById(R.id.nameET);
        balanceET = (EditText) findViewById(R.id.balanceET);
        goodsLV.setOnItemClickListener(new MyOnItemClickListener());

    }

    public void add(View view) {
        String name = nameET.getText().toString().trim();
        String balance = balanceET.getText().toString().trim();
        Goods goods = new Goods(name, balance.equals("") ? 0 : Integer.parseInt(balance));
        goodsdao.insert(goods);
        list.add(goods);
        adapter.notifyDataSetChanged();
        goodsLV.setSelection(goodsLV.getCount() - 1);
        nameET.setText("");
        balanceET.setText("");
    }

    private class MyAdapter extends BaseAdapter {

        @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;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View item = convertView != null ? convertView : View.inflate(getApplicationContext(), R.layout.item, null);
            TextView idTV = (TextView) item.findViewById(R.id.idTV);
            TextView nameTV = (TextView) item.findViewById(R.id.nameTV);
            TextView balanceTV = (TextView) item.findViewById(R.id.balanceTV);
            final Goods g = list.get(position);
            idTV.setText(g.getId() + "");
            nameTV.setText(g.getName());
            balanceTV.setText(g.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 view) {
                    g.setBalance(g.getBalance() + 1);
                    notifyDataSetChanged();
                    goodsdao.update(g);
                }
            });
            downIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    g.setBalance(g.getBalance() - 1);
                    notifyDataSetChanged();
                    goodsdao.update(g);
                }
            });
            deleteIV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    android.content.DialogInterface.OnClickListener listener = new android.content.DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialogInterface, int i) {
                            list.remove(g);
                            goodsdao.detele(g.getId());
                            notifyDataSetChanged();
                        }
                    };
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("确定要删除吗?");
                    builder.setPositiveButton("确定", listener);
                    builder.setNegativeButton("取消", null);
                    builder.show();
                }
            });
            return item;
        }
    }

    private class MyOnItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            Goods goods = (Goods) adapterView.getItemAtPosition(i);
            Toast.makeText(getApplicationContext(), goods.toString(), Toast.LENGTH_SHORT).show();
        }
    }
}

具体功能呢,自己摸索吧。。。。。。

小箭头上下可以调价格,小垃圾箱可以删除(还是说吧)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值