




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡介
1、Android實(shí)驗(yàn)報(bào)告一姓名:丁軍峰班級(jí):信科12-3學(xué)號(hào): 08123448一、 實(shí)驗(yàn)內(nèi)容編寫一個(gè) Android 應(yīng)用程序,實(shí)現(xiàn)對(duì)自己物品的管理, 功能包括添加、刪除和查詢等二、實(shí)驗(yàn)?zāi)康牧私?android 開發(fā)流程,掌握 SQLite數(shù)據(jù)庫和 ListView 控件的使用三、 需求分析使用 SQLite數(shù)據(jù)庫,使用 ListView 控件顯示物品四、實(shí)驗(yàn)過程1. 創(chuàng)建程序,設(shè)計(jì)用戶交互界面<?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=" 物品個(gè)數(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=" 物品價(jià)格 " /><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ù)量 (個(gè) )"+stuffInfos.get(position).getStuffAmount(); tv_price.setText(" 單價(jià) (元)"+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等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025年公共政策分析考試試卷及答案
- 汽車銷售及售后服務(wù)委托協(xié)議
- ××超市積分細(xì)則
- ××超市客戶反饋規(guī)定
- 蔬菜采購協(xié)議集合
- 2025年噴霧通風(fēng)冷卻塔項(xiàng)目申請(qǐng)報(bào)告
- 冬日的雪景銀裝素裹的自然風(fēng)光寫景13篇
- 讀一本成長小說后的體會(huì)作文(5篇)
- 2025年電工特種作業(yè)操作證考試試卷:電氣設(shè)備故障處理與預(yù)防措施實(shí)踐案例分析試題
- 2025年高品質(zhì)H酸項(xiàng)目立項(xiàng)申請(qǐng)報(bào)告
- 2024年江西省公安廳招聘警務(wù)輔助人員考試真題
- 2025年高考全國二卷數(shù)學(xué)高考真題解析 含參考答案
- 2025年日歷表全年(打印版)完整清新每月一張
- GB/T 4744-2013紡織品防水性能的檢測和評(píng)價(jià)靜水壓法
- 上海市學(xué)生健康促進(jìn)工程實(shí)施方案
- 實(shí)驗(yàn)室CNAS評(píng)審常見問題精編要點(diǎn)
- 通信管道施工三級(jí)-安全技術(shù)交底記錄表
- 橋梁荷載試驗(yàn)
- 綜合布線報(bào)價(jià)清單范本
- 礦山行業(yè)生產(chǎn)制造執(zhí)行系統(tǒng)(MES)
- 中學(xué)教師專業(yè)標(biāo)準(zhǔn)(試行)
評(píng)論
0/150
提交評(píng)論