时间:2021-05-19
一、 创建手机界面布局
创建一个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)最后的运行结果图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
今天给大家讲讲android的目录实现方法,就像大家看到的小说目录一样,android提供了ExpandableListView控件可以实现二级列表展示效果,现
本文实例为大家分享了Android实现搜索框展示的具体代码,供大家参考,具体内容如下展示效果代码区SouActivitypublicclassSouActivi
本文实例为大家分享了android实现节点进度条效果展示的具体代码,供大家参考,具体内容如代码:packageutils.android.view.lxz;im
本文实例为大家分享了Android实现时间倒计时功能展示的具体代码,供大家参考,具体内容如下效果展示MainActivity(主页面代码)publicclass
本文实例为大家分享了Android实现多条目加载展示的具体代码,供大家参考,具体内容如下展示效果这里写图片描述依赖testCompile'junit:junit