Android實(shí)現(xiàn)商品展示效果
一、 創(chuàng)建手機(jī)界面布局
創(chuàng)建一個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>
效果圖如下:
二、創(chuàng)建一個存儲展示的xml文件
創(chuàng)建一個名為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>
三、創(chuàng)建數(shù)據(jù)庫
在com.example.bz0209.shopshowdemo 的包下創(chuàng)建一個名為db的包,并在該包下定義一個dbHelper類繼承自SQLiteOpenHelper,創(chuàng)建數(shù)據(jù)庫代碼如下:
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) { } }
四、創(chuàng)建GoodsAdapter類
為了操作數(shù)據(jù)庫方便,創(chuàng)建一個GoodsAdapter類。因此需要在com.example.bz0209.shopshowdemo 的包下創(chuàng)建一個名為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; } }
五、創(chuàng)建數(shù)據(jù)操作邏輯類
在com.example.bz0209.shopshowdemo 的包下創(chuàng)建一個名為dao的包,并創(chuàng)建一個名為GoodsDao的類,用于操作數(shù)據(jù)。代碼如下:
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; } }
六、編寫界面交互代碼
數(shù)據(jù)庫操作完成后,需要界面與數(shù)據(jù)庫進(jìn)行交互,用于實(shí)現(xiàn)將數(shù)據(jù)庫中的數(shù)據(jù)以ListView的形式展現(xiàn)在界面上具體代碼如下:
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); } }
七、完成后的結(jié)果展示
(1)創(chuàng)建起數(shù)據(jù)庫的圖為:
(2)最后的運(yùn)行結(jié)果圖:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)的圓角按鈕、文字陰影按鈕效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)的圓角按鈕、文字陰影按鈕效果,涉及Android界面布局與屬性設(shè)置相關(guān)操作技巧,需要的朋友可以參考下2019-04-04Android中實(shí)現(xiàn)圓角圖片的幾種方法
本篇文章主要介紹了Android中實(shí)現(xiàn)圓角圖片的幾種方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-06-06純android代碼實(shí)現(xiàn)九宮格手勢密碼
這篇文章主要為大家詳細(xì)介紹了純android代碼實(shí)現(xiàn)九宮格手勢密碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-07-07Android中制作自定義dialog對話框的實(shí)例分享
這篇文章主要介紹了Android中制作自定義dialog對話框的實(shí)例分享,安卓自帶的Dialog顯然不夠用,因而我們要繼承Dialog類來制作自己的對話框,需要的朋友可以參考下2016-04-04Android中自定義View實(shí)現(xiàn)圓環(huán)等待及相關(guān)的音量調(diào)節(jié)效果
這篇文章主要介紹了Android中自定義View實(shí)現(xiàn)圓環(huán)等待及相關(guān)的音量調(diào)節(jié)效果,邏輯非常簡單,或許繪圖方面更加繁瑣XD 需要的朋友可以參考下2016-04-04Android Compose實(shí)現(xiàn)底部按鈕以及首頁內(nèi)容詳細(xì)過程
這篇文章主要介紹了如何利用compose框架制作app底部按鈕以及首頁內(nèi)容的詳細(xì)代碼,具有一定價(jià)值,感興趣的可以了解一下2021-11-11Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲功能
這篇文章主要為大家詳細(xì)介紹了Android SharedPreferences實(shí)現(xiàn)數(shù)據(jù)存儲功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06