亲宝软件园·资讯

展开

Android实现商品展示 Android实现商品展示效果

爱吃蛋糕的南宫滕逸 人气:0
想了解Android实现商品展示效果的相关内容吗,爱吃蛋糕的南宫滕逸在本文为您仔细讲解Android实现商品展示的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:Android,商品展示,下面大家一起来学习吧。

一、 创建手机界面布局

创建一个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>

效果图如下:

二、创建一个存储展示的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)创建起数据库的图为:

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

加载全部内容

相关教程
猜你喜欢
用户评论