收藏 分享(赏)

Android实验一实验报告.doc

上传人:精品资料 文档编号:8414413 上传时间:2019-06-25 格式:DOC 页数:22 大小:147.85KB
下载 相关 举报
Android实验一实验报告.doc_第1页
第1页 / 共22页
Android实验一实验报告.doc_第2页
第2页 / 共22页
Android实验一实验报告.doc_第3页
第3页 / 共22页
Android实验一实验报告.doc_第4页
第4页 / 共22页
Android实验一实验报告.doc_第5页
第5页 / 共22页
点击查看更多>>
资源描述

1、Android 实验报告一姓名:丁军峰班级:信科 12-3学号:08123448一、 实验内容编写一个 Android 应用程序,实现对自己物品的管理,功能包括添加、删除和查询等二、 实验目的了解 android 开发流程,掌握 SQLite 数据库和 ListView 控件的使用三、 需求分析使用 SQLite 数据库,使用 ListView 控件显示物品四、 实验过程1. 创建程序,设计用户交互界面2.创建 ListView Item 布局3.创建数据库package com.LIQI.Stuff;import android.content.Context;import android.

2、database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MySQLiteOpenHelper extends SQLiteOpenHelper public MySQLiteOpenHelper(Context context) super(context, “LIQI.db“, null, 1);/ TODO Auto-generated cons

3、tructor stubOverridepublic void onCreate(SQLiteDatabase db) db.execSQL(“create table LIQI(_id integer primary key autoincrement,stuff,amount,price)“);Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) / TODO Auto-generated method stub4.创建 account 类package com.LIQI.Stuff

4、;public class Stuff private String stuffName;private int StuffAmount;private float StuffPrice;public String getStuffName() return stuffName;public void setStuffName(String stuffName) this.stuffName = stuffName;public int getStuffAmount() return StuffAmount;public void setStuffAmount(int stuffAmount)

5、 StuffAmount = stuffAmount;public float getStuffPrice() return StuffPrice;public void setStuffPrice(float stuffPrice) StuffPrice = stuffPrice;5.创建数据操作逻辑package com.LIQI.Stuff;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.database.Cursor;import android.da

6、tabase.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageButton;import android.widget.ImageView;import android.widget.ListView;import android.widg

7、et.TextView;import android.widget.Toast;public class StuffList extends Activityprivate ListView lv;private SQLiteDatabase mDataBase;private ListstuffInfos;private MySQLiteOpenHelper mHelper;Overrideprotected void onCreate(Bundle savedInstanceState) setContentView(R.layout.show_all);mHelper=new MySQL

8、iteOpenHelper(this);lv=(ListView) findViewById(R.id.id_lv);all();super.onCreate(savedInstanceState);public void all()mDataBase=mHelper.getWritableDatabase();Cursor cursor=mDataBase.query(“LIQI“, null, null, null, null, null, null);stuffInfos=new ArrayList();while(cursor.moveToNext()Stuff stuff=new S

9、tuff();stuff.setStuffName(cursor.getString(1);stuff.setStuffAmount(cursor.getInt(2);stuff.setStuffPrice(cursor.getFloat(3);stuffInfos.add(stuff);cursor.close();mDataBase.close();lv.setAdapter( new Myadapter();class Myadapter extends BaseAdapterOverridepublic int getCount() return stuffInfos.size();O

10、verridepublic Object getItem(int position) / TODO Auto-generated method stubreturn null;Overridepublic long getItemId(int position) / TODO Auto-generated method stubreturn 0;Overridepublic View getView(final int position, View view , ViewGroup parent) view =View.inflate(StuffList.this, R.layout.stuf

11、f_item, null);TextView tv_name=(TextView) view .findViewById(R.id.id_tv_name);TextView tv_amount=(TextView)view . findViewById(R.id.id_tv_amount);TextView tv_price=(TextView) view .findViewById(R.id.id_tv_price);ImageButton iButton=(ImageButton) view.findViewById(R.id.id_ib_delete);tv_name.setText(s

12、tuffInfos.get(position).getStuffName();tv_amount.setText(“数量(个)“+stuffInfos.get(position).getStuffAmount();tv_price.setText(“单价 (元)“+stuffInfos.get(position).getStuffPrice();iButton.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) mDataBase=mHelper.getWritableDatabase();m

13、DataBase.delete(“LIQI“,“stuff=?“, new String stuffInfos.get(position).getStuffName();mDataBase.close();stuffInfos.remove(position);Myadapter.this.notifyDataSetChanged(););return view;6.编写界面交互代码package com.LIQI.Stuff;import android.app.Activity;import android.content.ContentValues;import android.cont

14、ent.Intent;import android.database.sqlite.SQLiteDatabase;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity implements OnClickLis

15、tener private MySQLiteOpenHelper mHelper;private SQLiteDatabase mDataBase;private Button bt_add,bt_all;private EditText et_stuffName,et_stuffAmount,et_stuffPrice;Overrideprotected void onCreate(Bundle savedInstanceState) super.onCreate(savedInstanceState);setContentView(R.layout.add_data);mHelper=ne

16、w MySQLiteOpenHelper(this);initView();initAction();public void initView()et_stuffName=(EditText) findViewById(R.id.id_et_stuffname);et_stuffAmount=(EditText) findViewById(R.id.id_et_stuffamount);et_stuffPrice=(EditText) findViewById(R.id.id_et_stuffprize);bt_add=(Button) findViewById(R.id.id_bt_add)

17、;bt_all=(Button) findViewById(R.id.id_bt_all);public void initAction()bt_add.setOnClickListener(this);bt_all.setOnClickListener(this);Overridepublic void onClick(View v) switch (v.getId() case R.id.id_bt_add:add();break;case R.id.id_bt_all:Intent intent=new Intent(this,StuffList.class);startActivity

18、(intent);break;public void add()String stuffName=et_stuffName.getText().toString().trim();int stuffAmount=Integer.parseInt(et_stuffAmount.getText().toString().trim();float stuffPrice=Float.parseFloat(et_stuffPrice.getText().toString().trim();Stuff stuff=new Stuff();stuff.setStuffName(stuffName);stuf

19、f.setStuffAmount(stuffAmount);stuff.setStuffPrice(stuffPrice);mDataBase=mHelper.getWritableDatabase();ContentValues contentValues=new ContentValues();contentValues.put(“stuff“, stuff.getStuffName();contentValues.put(“amount“, stuff.getStuffAmount();contentValues.put(“price“, stuff.getStuffPrice();long number=mDataBase.insert(“LIQI“, null,contentValues);if(number=-1)Toast.makeText(this, “添加失败“,1).show();else et_stuffAmount.setText(“);et_stuffName.setText(“);et_stuffPrice.setText(“);Toast.makeText(this, “添加成功,物品条数“+number,1).show();mDataBase.close();7.Manifest五、 实验结果程序界面显示

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 企业管理 > 管理学资料

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报