




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認(rèn)領(lǐng)
文檔簡介
1、Android實驗報告一姓名:丁軍峰班級:信科12-3學(xué)號: 08123448一、 實驗內(nèi)容編寫一個 Android 應(yīng)用程序,實現(xiàn)對自己物品的管理, 功能包括添加、刪除和查詢等二、實驗?zāi)康牧私?android 開發(fā)流程,掌握 SQLite數(shù)據(jù)庫和 ListView 控件的使用三、 需求分析使用 SQLite數(shù)據(jù)庫,使用 ListView 控件顯示物品四、實驗過程1. 創(chuàng)建程序,設(shè)計用戶交互界面<?xml version="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="an
2、droid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"android:orientation="vertical
3、"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" 物品清單 "/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"&g
4、t;<TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" 物品名稱 " /><EditTextandroid:id="+id/id_et_stuffname"android:layout_width="match_parent"android:layout_height="wrap_content"android:input
5、Type="text"/></ LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content&q
6、uot;android:text=" 物品個數(shù) " /><EditTextandroid:id="+id/id_et_stuffamount"android:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="text"/></ LinearLayout><LinearLayoutandroid:layout_width="match_pa
7、rent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text=" 物品價格 " /><EditTextandroid:id="+id/id_et_stuffprize"and
8、roid:layout_width="match_parent"android:layout_height="wrap_content"android:inputType="text"/></ LinearLayout></ LinearLayout><FrameLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><LinearLayoutand
9、roid: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="wrap_content"android:layout_weight="1"and
10、roid:text=" 添加記錄 "android:layout_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="6dp"android:layout_marginBottom= "7dp"android:gravity= "center"android:padding= "5dp"android:textColor="#727272"
11、;android:background="drawable/recordbutton"android:minHeight="0dp" /><Buttonandroid:id="+id/id_bt_all"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:text=" 顯示全部 "android:layout
12、_marginLeft="10dp"android:layout_marginRight="10dp"android:layout_marginTop="6dp"android:layout_marginBottom= "7dp"android:gravity= "center"android:padding= "5dp"android:textColor="#727272"android:background="drawable/recordb
13、utton"android:minHeight="0dp" /></ LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="#eee" /></ FrameLayout></ LinearLayout>2.創(chuàng)建 ListView Item布局<?xml version ="1.0"
14、encoding="utf-8"?><LinearLayoutxmlns:android="android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="horizontal"android:background="#ffffffff"><ImageViewandroid:id="+id/id_ig_name"androi
15、d: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"android:layout_width="0dp"android:layout_height="wrap_content&
16、quot;android:layout_weight="1"android:textColor="#000000"android:textSize="18sp"/><TextViewandroid:id="+id/id_tv_amount"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textColor
17、="#000000"android:textSize="18sp"/><TextViewandroid:id="+id/id_tv_price"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:textColor="#000000"android:textSize="18sp"/>
18、<ImageButtonandroid: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="#ffffffff"android:onClick= "deleteItem"/&
19、gt;</ LinearLayout>3.創(chuàng)建數(shù)據(jù)庫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 MySQLiteOpenHelper extends SQLiteOpenHelper pub
20、lic 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 keyautoincrement,stuff,amount,price)");Overridepublic void onUpgrade(
21、SQLiteDatabase db, int oldVersion, int newVersion) / TODO Auto-generated method stub4. 創(chuàng)建 account 類packagecom.LIQI.Stuff;publicclassStuff privateStringstuffName ;privateintStuffAmount;privatefloatStuffPrice;publicString getStuffName() returnstuffName ;publicvoidthissetStuffName(String stuffName) . s
22、tuffName= stuffName;publicintreturngetStuffAmount() StuffAmount;publicvoidsetStuffAmount(intstuffAmount) StuffAmount= stuffAmount;publicfloatreturngetStuffPrice() StuffPrice;publicvoidsetStuffPrice(floatstuffPrice) StuffPrice= stuffPrice;5. 創(chuàng)建數(shù)據(jù)操作邏輯package com.LIQI.Stuff;import java.util.ArrayList;i
23、mport 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.widg
24、et.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;private List<Stuff>stuffInfos;private MySQLiteOpenHelper mHelper;
25、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()mDataBase=mHelper.getWritableDatabase();Cursor cursor=mDataBase.query(&qu
26、ot;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.setStuffPrice(cursor.getFloat(3);stuffInfos.add(stuff);cursor.close();mDataBa
27、se.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;Overridepublic long getItemId(int position) / TODO Auto-generated method stub
28、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=(TextView)view . findViewById(R.id.id_tv_amount);TextView tv_price=(
29、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ù)量 (個 )"+stuffInfos.get(position).getStuffAmount(); tv_price.setText(" 單價 (元)"+stuffIn
30、fos.get(position).getStuffPrice();iButton.setOnClickListener(new OnClickListener() Overridepublic void onClick(View v) mDataBase=mHelper.getWritableDatabase();mDataBase.delete("LIQI","stuff=?", new String stuffInfos.get(position).getStuffName();mDataBase.close();stuffInfos.remove
31、(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;import android.os.Bundle;import android.view.View;import andro
32、id.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
33、 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 void initView()et_stuffName=(EditText) findViewById(R.id.id_e
34、t_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 initAction()bt_add.setOnClickListener(this);bt_all.setOnClick
35、Listener(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_stuffName.getText().toString().trim();int stuffAmount=Integer.parse
36、Int(et_stuffAmount.getText().toString().trim();floatstuffPrice=Float.parseFloat(et_stuffPrice.getText().toString().trim();Stuff stuff=new Stuff();stuff.setStuffName(stuffName);stuff.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", stu
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 公務(wù)員協(xié)議書范本
- 環(huán)保產(chǎn)業(yè)代收款合作協(xié)議范本
- 高新技術(shù)產(chǎn)業(yè)園區(qū)廠房租賃安全合同樣本
- 特色美食街三股東合作協(xié)議及區(qū)域經(jīng)濟發(fā)展貢獻
- 車輛租賃合同保證金與車輛租賃企業(yè)社會責(zé)任履行協(xié)議
- 企業(yè)參股合作協(xié)議書范本
- 生物質(zhì)能源財產(chǎn)抵押合同
- 城市交通車輛翻新烤漆租賃合作協(xié)議
- 餐廳餐飲員工離職合同范本
- 廢舊鋼模板翻新工藝技術(shù)方案
- 鐵路車輛-鐵路車輛的運用與檢修
- 火鍋店領(lǐng)班的崗位職責(zé)和工作流程
- 二人合伙協(xié)議書(電子版)
- 上門廚師項目商業(yè)計劃書
- 第35屆中國化學(xué)奧林匹克(初賽競賽)試題及參考答案
- 許可證有效期內(nèi)輻射安全和防護工作總結(jié)
- 四川省中小流域暴雨洪水計算表格(尾礦庫洪水計算)
- 山東大學(xué)齊魯醫(yī)學(xué)院
- 椅子部件圖紙
- 街道綜合協(xié)管員筆試題
- 入庫單(標(biāo)準(zhǔn)范本)
評論
0/150
提交評論