Android實(shí)驗(yàn)一實(shí)驗(yàn)報(bào)告_第1頁(yè)
Android實(shí)驗(yàn)一實(shí)驗(yàn)報(bào)告_第2頁(yè)
Android實(shí)驗(yàn)一實(shí)驗(yàn)報(bào)告_第3頁(yè)
Android實(shí)驗(yàn)一實(shí)驗(yàn)報(bào)告_第4頁(yè)
Android實(shí)驗(yàn)一實(shí)驗(yàn)報(bào)告_第5頁(yè)
已閱讀5頁(yè),還剩19頁(yè)未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、Android 實(shí)驗(yàn)報(bào)告一:丁軍峰班級(jí):信科12-3學(xué)號(hào):08123448學(xué)習(xí)文檔 僅供參考學(xué)習(xí)文檔僅供參考實(shí)驗(yàn)內(nèi)容編寫一個(gè) Android 應(yīng)用程序,實(shí)現(xiàn)對(duì)自己物品的管理,功能包括android:layout_height="0dp"學(xué)習(xí)文檔 僅供參考添加、刪除和查詢等實(shí)驗(yàn)?zāi)康牧私鈇ndroid開(kāi)發(fā)流程,掌握SQLite數(shù)據(jù)庫(kù)和ListView控件的使三、需求分析使用SQLite數(shù)據(jù)庫(kù),使用ListView控件顯示物品四、實(shí)驗(yàn)過(guò)程1. 創(chuàng)建程序,設(shè)計(jì)用戶交互界面<? xml version = "1.0" encoding ="utf-

2、8"?>/apk/res/android"<LinearLayout xmlns:androidandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation= "vertical"<LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content&qu

3、ot;android:layout_weight ="1"android:orientation = "vertical" ><TextViewandroid:layout_width ="wrap_content"android:layout_height ="wrap_content"android:text =" 物品清單 "/><LinearLayoutandroid:layout_width="match_parent"android:lay

4、out_height="wrap_content"android:orientation= "horizontal" >學(xué)習(xí)文檔 僅供參考<TextViewandroid:layout_width ="wrap_content"android:layout_height ="wrap_content" android:text =" 物品名稱 "/><EditTextandroid:id ="+id/id_et_stuffname"android:la

5、yout_width ="match_parent"android:layout_height ="wrap_content" android:inputType ="text" /></ LinearLayout >android:layout_width<LinearLayout="match_parent"android:orientation<TextView= "horizontal" >android:layout_width="wrap

6、_content"android:layout_height="wrap_content"android:text =" 物品個(gè)數(shù) "/><EditTextandroid:id ="+id/id_et_stuffamount"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="text" /></ LinearLay

7、out ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation= "horizontal" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text =" 物品價(jià)格 "/&

8、gt;<EditTextandroid:id ="+id/id_et_stuffprize"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="text" /></ LinearLayout </ LinearLayout ><FrameLayoutandroid:layout_width="match_parent"android:

9、layout_height="wrap_content"<LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation= "horizontal" ><Buttonandroid:id ="+id/id_bt_add"android:layout_width ="0dp"android:layout_height=&

10、quot;wrap_content"android:layout_weight="1"android:text =" 添加記錄 "android:layout_marginLeft ="10dp"android:layout_marginRight ="10dp"android:layout_marginTop ="6dp"android:layout_marginBottom = "7dp"android:gravity ="center"an

11、droid:padding ="5dpandroid:textColor="#727272"學(xué)習(xí)文檔僅供參考android:paddingandroid:textColorandroid:backgroundandroid:minHeight</ LinearLayout ><Viewandroid:layout_widthandroid:layout_heightandroid:backgroundandroid:background="drawable/recordbutton"android:minHeight =&qu

12、ot;0dp" /><Buttonandroid:id ="+id/id_bt_all"android:layout_width ="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text =" 顯示全部 "android:layout_marginLeft ="10dp"android:layout_marginRight ="10dp&quo

13、t;android:layout_marginTop ="6dp"android:layout_marginBottom = "7dp"android:gravity ="center" ="5dp"="#727272"="drawable/recordbutton"="0dp" />="match_parent"="1dp"="#eee" /></ LinearLayout2.

14、創(chuàng)建 ListView Item 布局<?xml version ="1.0" encoding ="utf-8"?><LinearLayout xmlns:android =" /apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation= "horizontal"android:background=&

15、quot;#ffffffff"<ImageViewandroid:id ="+id/id_ig_name" android:layout_width ="0dp"android:layout_height="wrap_content"android:layout_weight="1"/>android:src ="drawable/ic_menu_paste_holo_light"<TextViewandroid:id ="+id/id_tv_name&qu

16、ot;android:layout_width ="0dp"android:layout_height="wrap_content'android:layout_weight="1"android:textC010r="#000000”學(xué)習(xí)文檔 僅供參考android:textSize ="18sp" /><TextViewandroid:id ="+id/id_tv_amount”android:layout_width ="0dp"android:layout_

17、height="wrap_contentandroid:layout_weight="1"android:textColor ="#000000"android:textSize ="18sp" /><TextViewandroid:id ="+id/id_tv_price”android:layout_width ="0dp"android:layout_height="wrap_contentandroid:layout_weight="1"andr

18、oid:textColor ="#000000"android:textSize ="18sp" /><ImageButton _android:id ="+id/id_ib_delete”android:layout_width ="0dp"android:layout_height="wrap_content”android:layout_weight="1"android:src ="drawable/ic_menu_delete”android:background

19、 ="#ffffffff"android:onClick="deleteItem”/>3. 創(chuàng)建數(shù)據(jù)庫(kù)package com.LIQI.Stuff;import android.content.Context;import android.database.sqlite.SQLiteDatabase;import android.database.sqlite.SQLiteDatabase.CursorFactory;import android.database.sqlite.SQLiteOpenHelper;public class MySQLiteOp

20、enHelper extends SQLiteOpenHelper public MySQLiteOpenHelper(Context context) super(context, "LIQI.db", null, 1);/ TODO Auto-generated constructor stubOverridepublic void onCreate(SQLiteDatabase db) db.execSQL("create table LIQI(_id integer primary key autoincrement,stuff,amount,price)

21、");Overridepublic void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) / TODO Auto-generated method stub4. 創(chuàng)建 account 類package com.LIQI.Stuff;public class Stuff private String stuffName ;privateintStuffAmount;privatefloat StuffPrice;public String getStuffName() return stuffName ;pu

22、blic void setStuffName(String stuffName) this . stuffName = stuffName;public int getStuffAmount() return StuffAmount ;public void setStuffAmount( int stuffAmount) StuffAmount = stuffAmount;public float getStuffPrice() return StuffPrice ;public void setStuffPrice( float stuffPrice) StuffPrice = stuff

23、Price;5. 創(chuàng)建數(shù)據(jù)操作邏輯package com.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.

24、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 Activityprivate ListView lv;private SQLiteDatabase mDataBase;pri

25、vate List<Stuff>stuffInfos;private MySQLiteOpenHelper mHelper;Overrideprotected void onCreate(Bundle savedInstanceState) setContentView(R.layout.show_all);mHelper=new MySQLiteOpenHelper(this);lv=(ListView) findViewById(R.id.id_lv);all();super.onCreate(savedInstanceState);public void all()mData

26、Base=mHelper.getWritableDatabase();Cursor cursor=mDataBase.query("LIQI", null, null, null, null, null, null);stuffInfos=new ArrayList<Stuff>();while(cursor.moveToNext()Stuff stuff=new Stuff();stuff.setStuffName(cursor.getString(1);stuff.setStuffAmount(cursor.getInt(2);stuff.setStuffP

27、rice(cursor.getFloat(3);stuffInfos.add(stuff);cursor.close();mDataBase.close();lv.setAdapter( new Myadapter();class Myadapter extends BaseAdapterOverridepublic int getCount() return stuffInfos.size();Overridepublic Object getItem(int position) / TODO Auto-generated method stub return null;Overridepu

28、blic long getItemId(int position) / TODO Auto-generated method stub return 0;Overridepublic View getView(final int position, View view , ViewGroup parent) view =View.inflate(StuffList.this, R.layout.stuff_item, null);TextView tv_name=(TextView) view .findViewById(R.id.id_tv_name);TextView tv_amount=

29、(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(stuffInfos.get(position).getStuffName();tv_amount.setText("數(shù)量(個(gè))"+stuffInfos.get(positio

30、n).getStuffAmount();tv_price.setText("單彳(元)"+stuffInfos.get(position).getStuffPrice();iButton.setOnClickListener(new OnClickListener() Override public void onClick(View v) mDataBase=mHelper.getWritableDatabase();mDataBase.delete("LIQI","stuff=?",newStringstuffInfos.get(

31、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.content.Intent;import android.database.sqlite.SQLiteDatabase;im

32、port 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 SQLiteData

33、base 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=new MySQLiteOpenHelper(this);initView();initAction();public v

34、oid 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);bt_all=(Button) findViewById(R.id.id_bt_all);public void i

35、nitAction()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(intent);break;public void add()String stuffName=et_stuffNa

36、me.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);stuff.setStuffAmount(stuffAmount);stuff.setStuffPrice(stuffPric

37、e);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).sho

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論