Android实验一实验报告

 A ndr oid 实验报告 一

  姓名: : 丁军峰

 班级:信科12 2 —3 3

 学号: : 08 12 34 48

 一、

 实验 内容

  编写一个Android 应用程序,实现对自己物品得管理,功能包括添加、删除与查询等 二、

 实验 目得

  了解android 开发流程,掌握SQLite 数据库与 ListView 控件得使用 三、

 需求 分析 使用 SQLite 数据库,使用 ListView 控件显示物品 四、

 实验 过程 1. 创建 程序, 设计 用户交互界面

 〈?xml version=" 1、 0” encoding=” u t f —8”?> 〈LinearLayout xmlns:android="”

  android:layout_width=” mat c h_ pare n t”

  android:layout_height= " m at c h _par e nt"

  android:orientation=” ve rt i c a l” 〉

  <LinearLayout

  android:layout_width="mat c h _ p a rent"

  android:layout_height=”0d p "

  android:layout_weight="1"

 android:orientation=” ve rtica l ">

  <TextView

 android:layout_width=" w rap_c on te n t"

  android:layout_height="wrap_ c on t ent”

  android:text=" 物品清单 "

  />

  <LinearLayout

 android:layout_width=" match _ p aren t ”

  android:layout_height=” w r a p_ co nt e n t "

  android:orientation=” h or i zont a l " >

  <TextView

 android:layout_width="wra p_ cont e nt"

  android:layout_height=” w rap_content”

  android:text=" 物品名称" />

  〈EditText

 android:id="+ id /id_e t_ stu f fnam e "

  android:layout_width=”mat c h _ p a re nt "

  android:layout_height= " wrap_con t ent”

 android:inputType=”text"/>

  </LinearLayout〉

  〈LinearLayout

 android:layout_width= " m a t c h_parent”

  android:layout_height=”wr ap _c o ntent”

  android:orientation=”ho r i z ontal”〉

  <TextView

 android:layout_width=”wrap_c o ntent”

  android:layout_height="wr a p_conte n t"

 android:text=" 物品个数 ”/〉

  <EditText

 android:id=" + i d /id _e t _s tuffam o un t"

 android:layout_width=” mat ch_p a rent”

  android:layout_height=”wra p _con t ent”

 android:inputType="tex t ”/>

  </LinearLayout〉

  <LinearLayout

 android:layout_width=” ma t c h_par e nt”

  android:layout_height=” w ra p_c ontent”

  android:orientation=”horizont al" 〉

  〈TextView

 android:layout_width=" wr ap_c o nt e nt"

  android:layout_height="wrap _ conten t ”

  android:text= "物品价格 ”/〉

  <EditText

 android:id="+id / id_et _ stuff p ri ze ”

  android:layout_width=" m atc h _ p ar e nt”

  android:layout_height=" w ra p _content "

  android:inputType=”tex t "/〉

  </LinearLayout〉

  </LinearLayout>

  <FrameLayout

 android:layout_width=" ma tch_pa r ent"

  android:layout_height=" wr ap _ c o n t ent”

 >

  〈LinearLayout

 android:layout_width=” m atch_ p arent”

  android:layout_height="m a tch_p a rent"

  android:orientation= " hori z on t al"〉

 〈Button

 android:id="+i d /id_b t _ a dd”

  android:layout_width=”0dp"

  android:layout_height="wra p _c o ntent"

  android:layout_weight= " 1"

  android:text=" 添加记录 "

 android:layout_marginLeft=” 1 0d p ”

  android:layout_marginRight=” 10 dp"

  android:layout_marginTop=”6dp"

  android:layout_marginBottom=" 7 d p "

  android:gravity="cen ter ”

  android:padding=” 5d p”

  android:textColor=" #7 27272”

  android:background=” d ra wa bl e /r e cord b utt o n"

  android:minHeight=”0 d p”/>

  〈Button

  android:id="+id/id_bt_ a ll"

  android:layout_width=”0dp”

  android:layout_height=”w r ap_cont e n t"

 android:layout_weight=”1”

 android:text=" 显示全部 "

  android:layout_marginLeft=”10 d p"

  android:layout_marginRight=”1 0 d p "

  android:layout_marginTop=" 6 dp”

  android:layout_marginBottom=”7dp"

  android:gravity=”cen t er”

  android:padding=”5dp"

  android:textColor="#72727 2 "

  android:background=" d r aw ab l e/ r ecordbut t on"

  android:minHeight=”0d p ” /〉

  </LinearLayout>

  〈View

 android:layout_width=”match_parent”

  android:layout_height=” 1 dp"

  android:background=" #e ee"/>

  </FrameLayout〉 〈/LinearLayout> 2 、 创建 Li stView I tem 布局

 <?xml version=”1 、 0” encoding= "ut f-8”?> 〈LinearLayout xmlns:android=””

  android:layout_width="m a t c h_p a r e nt "

 android:layout_height=" m atch _ parent "

 android:orientation= " hor i z o n ta l”

 android:background=”# f f f ffff f ”>

 <ImageView

 android:id="+ i d/id_ig_name "

 android:layout_width="0dp”

  android:layout_height=”wrap_co ntent ”

  android:layout_weight="1”

  android:src="dr a w ab le/ i c _ m e nu_ p aste_holo _ lig h t”/〉

  <TextView

  android:id= " +i d/id_ tv_na m e"

  android:layout_width=”0dp"

  android:layout_height="w ra p_co n te n t”

  android:layout_weight="1"

  android:textColor="#000 0 00”

  android:textSize=” 1 8sp”/>

  <TextView

 android:id=”+id/ i d_tv _ a m o u n t "

  android:layout_width=” 0dp ”

  android:layout_height="wra p _ c o nt ent"

  android:layout_weight=”1"

  android:textColor=" # 0 000 00"

  android:textSize=”1 8 s p ”/>

  〈TextView

 android:id=”+id / id_ t v _ p r ice”

  android:layout_width=”0d p ”

  android:layout_height=”wr a p_ c on t en t"

 android:layout_weight=”1”

 android:textColor=” # 000 0 00 "

 android:textSize="1 8 sp"/>

  <ImageButton

 android:id=”+id/id _i b_delete”

  android:layout_width=”0dp"

  android:layout_height=”wrap _ con t ent”

  android:layout_weight=”1”

  android:src=”drawable/ic_men u _dele t e”

  android:background="#f f ffffff”

  android:onClick= "del eteItem"/> 〈/LinearLayout〉 3 、创建 数据库

  package 、LIQI、Stuff; import android、content、Context; import android、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 constructor stub

 } ﻩ Override

 { )bd esabataDetiLQS(etaerCno diov cilbupﻩ ﻩ ,tnemercniotua

 yek yramirp regetni di_(IQIL elbat etaerc"(LQScexe、bdﻩstuff,amount,price)");

 }

  edirrevOﻩ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)

 {

 // ﻩ

 buts dohtem detareneg-otuA ODOTﻩ } } 4 、创建 acc ot unt 类

 pac kage 、LIQI、Stuff; publi c class Stuff { p rivate String stuffName; privat e i nt

 StuffAmount; private floa t StuffPrice; pu bli c String getStuffName() {

 nruterﻩ rn stuffName; } publi c void setStuffName(String stuffName) {

 tﻩ thi s、stuffName = stuffName; } public int getStuffAmount() {

 terﻩ eturn StuffAmount; }

 public voi d setStuffAmount(int stuffAmount) {

 StuffAmount = stuffAmount; } pub lic float getStuffPrice() {

 re turn StuffPrice; } publi c voi d setStuffPrice( flo at stuffPrice) {

  ;ecirPffuts = ecirPffutSﻩ} } 5 、创建 数据操作逻辑

 package 、LIQI、Stuff; import java、util、ArrayList; import java、util、List; import android、app、Activity; import android、database、Cursor; import android、database、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、widget、TextView; import android、widget、Toast; public class StuffList extends Activity{

  ;vl weiVtsiL etavirpﻩ

 ;esaBataDm esabataDetiLQS etavirpﻩ

 private List〈Stuff>stuffInfos;

  ;repleHm repleHnepOetiLQSyM etavirpﻩ

 Override protected void onCreate(Bundle savedInstanceState)

 {

  ;)lla_wohs、tuoyal、R(weiVtnetnoCtesﻩ

 ;)siht(repleHnepOetiLQSyM wen=repleHmﻩ

 ;)vl_di、di、R(dIyBweiVdnif )weiVtsiL(=vlﻩ all();

 super、onCreate(savedInstanceState); }

  public void all(){

 mDataBase=mHelper、getWritableDatabase();

 ﻩ un ,llun ,llun ,llun ,llun ,”IQIL”(yreuq、esaBataDm=rosruc rosruCﻩll, null);

  ﻩ stuffInfos=new ArrayList<Stuff〉();

  ﻩ while(cursor、moveToNext()){

  ﻩ

 Stuff stuff=new Stuff();

 ;))1(gnirtSteg、rosruc(emaNffutStes、ffutsﻩ ﻩ ﻩ

 stuff、setStuffAmount(cursor、getInt(2));

  ﻩ ﻩ

 stuff、setStuffPrice(cursor、getFloat(3));

 ﻩ ﻩ stuffInfos、add(stuff);

 ﻩ ﻩ }

 cursor、close();

 ﻩ ﻩ mDataBase、close();

 ﻩ

 ;))(retpadayM wen (retpadAtes、vlﻩ }

 class Myadapter extends BaseAdapter{

 ﻩ

 edirrevOﻩ ﻩ

 { )(tnuoCteg tni cilbupﻩ

 ﻩ

 ;)(ezis、sofnIffuts nruterﻩ } ﻩ

 Override

  public Object getItem(int position) {

 // ﻩ ﻩ

 buts dohtem detareneg-otuA ODOTﻩ

 ﻩ return null;

  }

  edirrevOﻩ

  { )noitisop tni(dImetIteg gnol cilbupﻩ

 // ﻩ

 buts dohtem detareneg—otuA ODOTﻩ ﻩ

 ;0 nruterﻩ } ﻩ ﻩ

 edirrevOﻩ ﻩ

 { )tnerap puorGweiV , weiv weiV ,noitisop tni lanif(weiVteg weiV cilbupﻩ

 view =View、inflate(StuffList、this, R、layout、stuff_item,

 null);

 ﻩ

 ;)eman_vt_di、di、R(dIyBweiVdnif、 weiv )weiVtxeT(=eman_vt weiVtxeTﻩ

 ﻩ TextView tv_amount=(TextView)view 、 findViewById(R、id、id_tv_amount);

 ﻩ

 ;)ecirp_vt_di、di、R(dIyBweiVdnif、 weiv )weiVtxeT(=ecirp_vt weiVtxeTﻩ

  ImageButton iButton=(ImageButton) view、findViewById(R、id、id_ib_delete);

 ﻩ

 ;))(emaNffutSteg、)noitisop(teg、sofnIffuts(txeTtes、eman_vtﻩ

 ﻩ tv_amount、setText("数量(个)”+stuffInfos、get(position)、getStuffAmount());

 ﻩ ﻩ rPffutSteg、)noitisop(teg、sofnIffuts+")元(价单"(txeTtes、ecirp_vtﻩice());

  iButton、setOnClickListener(new OnClickListener() {

 ﻩ ﻩ ﻩ Override

 ﻩ

 { )v weiV(kcilCno diov cilbupﻩ

 ﻩ

  mDataBase=mHelper、getWritableDatabase();

 ﻩ ﻩ op(teg、sofnIffuts{][ gnirtS wen ,"?=ffuts”,"IQIL”(eteled、esaBataDmﻩsition)、getStuffName()});

 ﻩ ﻩ

 ﻩ

 ;)(esolc、esaBataDmﻩ

 ﻩ ﻩ ﻩ

 ;)noitisop(evomer、sofnIffutsﻩ

 ﻩ

 ﻩ Myadapter、this、notifyDataSetChanged();

  ﻩ

 } ﻩ

  ;)} ﻩ

 ﻩ

 ;weiv nruterﻩ } ﻩ ﻩ } ﻩ}

 6 6 、编写 界面交互代码

 package 、LIQI、Stuff; import android、app、Activity; import android、content、ContentValues; import android、content、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 OnClickListener {

  private MySQLiteOpenHelper mHelper;

  private SQLiteDatabase mDataBase;

  private Button bt_add,bt_all;

  private EditText et_stuffName,et_stuffAmount,et_stuffPrice;

  edirrevOﻩ protected void onCreate(Bundle savedInstanceState) {

  super、onCreate(savedInstanceState);

  ;)atad_dda、tuoyal、R(weiVtnetnoCtesﻩ

  ;)siht(repleHnepOetiLQSyM wen=repleHmﻩ

 ;)(weiVtiniﻩ

  ;)(noitcAtiniﻩ

  }

 public void initView(){

  ;)emanffuts_te_di、di、R(dIyBweiVdnif )txeTtidE(=emaNffuts_teﻩ

 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);

  ;)lla_tb_di、di、R(dIyBweiVdnif )nottuB(=lla_tbﻩ

  }

 public void initAction(){

  bt_add、setOnClickListener(this);

  bt_all、setOnClickListener(this);

 }

  edirrevOﻩ

 { )v weiV(kcilCno diov cilbupﻩ

 switch (v、getId())

 {

 :dda_tb_di、di、R esacﻩ

 ;)(ddaﻩ

 ﻩ break;

 ﻩ

 :lla_tb_di、di、R esacﻩ

 ;)ssalc、tsiLffutS,siht(tnetnI wen=tnetni tnetnIﻩ

 ;)tnetni(ytivitcAtratsﻩ

  ﻩ ;kaerbﻩ

 } ﻩ } ﻩ

  public void add(){

 ﻩ

 ;)(mirt、)(gnirtSot、)(txeTteg、emaNffuts_te=emaNffuts gnirtSﻩ 、)(gnirtSot、)(txeTteg、tnuomAffuts_te(tnIesrap、regetnI=tnuomAffuts tniﻩtrim());

  float

 stuffPrice=Float、parseFloat(et_stuffPrice、getText()、toString()、trim());

  Stuff stuff=new Stuff();

 ﻩ

 ;)emaNffuts(emaNffutStes、ffutsﻩ

 ;)tnuomAffuts(tnuomAffutStes、ffutsﻩ

  ;)ecirPffuts(ecirPffutStes、ffutsﻩ

 ;)(esabataDelbatirWteg、repleHm=esaBataDmﻩ ﻩ

 ;)(seulaVtnetnoC wen=seulaVtnetnoc seulaVtnetnoCﻩ

 ;))(emaNffutSteg、ffuts ,"ffuts"(tup、seulaVtnetnocﻩ

 contentValues、put(”amount", stuff、getStuffAmount());

  contentValues、put("price”, stuff、getStuffPrice());

 ;)seulaVtnetnoc,llun ,"IQIL"(tresni、esaBataDm=rebmun gnolﻩ ﻩ

 {)1-==rebmun(fiﻩ

 ;)(wohs、)1,”败失加添” ,siht(txeTekam、tsaoTﻩ }

 { esleﻩ

 ﻩ et_stuffAmount、setText("");

  ;)""(txeTtes、emaNffuts_teﻩ

 ;)"”(txeTtes、ecirPffuts_teﻩ

 ﻩ Toast、makeText(this, ”添加成功,物品条数"+number,1)、show();

  } ﻩ

  ;)(esolc、esaBataDmﻩ }

 } 7 7 、Ma a n ifest

 <?xml version="1 、 0" encoding=” u t f —8”?> 〈manifest xmlns:android=”"

  package=" 、 LIQI 、 St uf f”

  android:versionCode="1”

  android:versionName="1 、0 ” >

  〈uses—sdk

  android:minSdkVersion="8 "

 android:targetSdkVersion=”1 7 ” /〉

  <application

  android:allowBackup= " true”

  android:icon="d raw abl e /ic_ l a un cher"

  android:label="string/a p p_n a m e "

 >

  <activity

  android:name=” 、 Ma inA c tivi t y ”

  android:label=”st ri ng/ a pp_n am e” 〉

  〈intent-filter〉

  <action android:name=” a nd r oid 、 intent 、 action 、M AI N "

 />

  〈category android:name=” a ndroi d、i ntent 、 ca tegory 、L AU N C H ER" /〉

  </intent—filter〉

  〈/activity〉

 <activity android:name=” 、L IQI 、 Stu f f 、 St uf fLi s t " 〉</activity〉

  </application〉 </manifest> 五、

 实验 结果

 程序界面显示