欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android实现商品展示效果

程序员文章站 2023-12-10 19:10:22
一、 创建手机界面布局 创建一个activity_main.xml文件代码如下:

一、 创建手机界面布局

创建一个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="com.example.bz0209.shopshowdemo.activity_main"
 android:orientation="vertical">
 <linearlayout
 android:layout_width="match_parent"
 android:layout_height="wrap_content">

 <edittext
 android:id="@+id/etname"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:hint="商品名称"
 android:inputtype="text"
 android:layout_weight="1"
 />
 <edittext
 android:id="@+id/etamount"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:hint="商品金额"
 android:inputtype="number"
 android:layout_weight="1"
 />
 <imageview
 android:id="@+id/ivadd"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:onclick="addgoods"
 android:inputtype="text"
 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>

效果图如下:

Android实现商品展示效果

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

创建一个名为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:text="1"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/tvid"
 android:textsize="20sp"
 android:layout_weight="2"

 />
 <textview
 android:text="商品名称"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/tvname"
 android:textsize="20sp"
 android:layout_weight="2"

 />
 <textview
 android:text="金额"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:id="@+id/tvamount"
 android:textsize="20sp"
 android:layout_weight="2"
 />
 <imageview
  android:id="@+id/ivup"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@android:drawable/arrow_up_float"
  />
 <imageview
  android:id="@+id/ivdown"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@android:drawable/arrow_down_float"
  />
 <imageview
  android:id="@+id/ivdelete"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content"
  android:src="@android:drawable/arrow_down_float"
  />
</linearlayout>

三、创建数据库

在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<goods>{
 private int resounceid;

 public goodsadapter(context context, int resource, list<goods> 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<goods> queryall(){
 list<goods>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(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 list<goods>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);
 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实现商品展示效果

Android实现商品展示效果

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

Android实现商品展示效果

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