Android实验一实验报告.docx

上传人:b****6 文档编号:6970558 上传时间:2023-01-13 格式:DOCX 页数:15 大小:141.03KB
下载 相关 举报
Android实验一实验报告.docx_第1页
第1页 / 共15页
Android实验一实验报告.docx_第2页
第2页 / 共15页
Android实验一实验报告.docx_第3页
第3页 / 共15页
Android实验一实验报告.docx_第4页
第4页 / 共15页
Android实验一实验报告.docx_第5页
第5页 / 共15页
点击查看更多>>
下载资源
资源描述

Android实验一实验报告.docx

《Android实验一实验报告.docx》由会员分享,可在线阅读,更多相关《Android实验一实验报告.docx(15页珍藏版)》请在冰豆网上搜索。

Android实验一实验报告.docx

Android实验一实验报告

 

Android实验报告一

 

丁军峰

班级:

信科12-3

学号:

08123448

一、实验容

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

二、实验目的

了解android开发流程,掌握SQLite数据库和ListView控件的使用

三、需求分析

使用SQLite数据库,使用ListView控件显示物品

四、实验过程

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

xmlversion="1.0"encoding="utf-8"?

>

android="schemas.android./apk/res/android"

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="vertical">

android:

layout_width="match_parent"

android:

layout_height="0dp"

android:

layout_weight="1"

android:

orientation="vertical">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="物品清单"

/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="物品名称"/>

android:

id="+id/id_et_stuffname"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

inputType="text"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="物品个数"/>

android:

id="+id/id_et_stuffamount"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

inputType="text"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

orientation="horizontal">

android:

layout_width="wrap_content"

android:

layout_height="wrap_content"

android:

text="物品价格"/>

android:

id="+id/id_et_stuffprize"

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

android:

inputType="text"/>

android:

layout_width="match_parent"

android:

layout_height="wrap_content"

>

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="horizontal">

android:

id="+id/id_bt_add"

android:

layout_width="0dp"

android:

layout_height="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"

android:

padding="5dp"

android:

textColor="#727272"

android:

background="drawable/recordbutton"

android:

minHeight="0dp"/>

android:

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"

android:

layout_marginTop="6dp"

android:

layout_marginBottom="7dp"

android:

gravity="center"

android:

padding="5dp"

android:

textColor="#727272"

android:

background="drawable/recordbutton"

android:

minHeight="0dp"/>

android:

layout_width="match_parent"

android:

layout_height="1dp"

android:

background="#eee"/>

 

2.创建ListViewItem布局

xmlversion="1.0"encoding="utf-8"?

>

android="schemas.android./apk/res/android"

android:

layout_width="match_parent"

android:

layout_height="match_parent"

android:

orientation="horizontal"

android:

background="#ffffffff">

android:

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"/>

android:

id="+id/id_tv_name"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

textColor="#000000"

android:

textSize="18sp"/>

android:

id="+id/id_tv_amount"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

textColor="#000000"

android:

textSize="18sp"/>

android:

id="+id/id_tv_price"

android:

layout_width="0dp"

android:

layout_height="wrap_content"

android:

layout_weight="1"

android:

textColor="#000000"

android:

textSize="18sp"/>

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="#ffffffff"

android:

onClick="deleteItem"/>

3.创建数据库

package.LIQI.Stuff;

importandroid.content.Context;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.database.sqlite.SQLiteDatabase.CursorFactory;

importandroid.database.sqlite.SQLiteOpenHelper;

publicclassMySQLiteOpenHelperextendsSQLiteOpenHelper{

publicMySQLiteOpenHelper(Contextcontext

){

super(context,"LIQI.db",null,1);

//TODOAuto-generatedconstructorstub

}

Override

publicvoidonCreate(SQLiteDatabasedb){

db.execSQL("createtableLIQI(_idintegerprimarykeyautoincrement,stuff,amount,price)");

}

Override

publicvoidonUpgrade(SQLiteDatabasedb,intoldVersion,intnewVersion){

//TODOAuto-generatedmethodstub

}

}

4.创建account类

package.LIQI.Stuff;

publicclassStuff{

privateStringstuffName;

privateintStuffAmount;

privatefloatStuffPrice;

publicStringgetStuffName(){

returnstuffName;

}

publicvoidsetStuffName(StringstuffName){

this.stuffName=stuffName;

}

publicintgetStuffAmount(){

returnStuffAmount;

}

publicvoidsetStuffAmount(intstuffAmount){

StuffAmount=stuffAmount;

}

publicfloatgetStuffPrice(){

returnStuffPrice;

}

publicvoidsetStuffPrice(floatstuffPrice){

StuffPrice=stuffPrice;

}

}

5.创建数据操作逻辑

package.LIQI.Stuff;

importjava.util.ArrayList;

importjava.util.List;

importandroid.app.Activity;

importandroid.database.Cursor;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.view.ViewGroup;

importandroid.widget.BaseAdapter;

importandroid.widget.ImageButton;

importandroid.widget.ImageView;

importandroid.widget.ListView;

importandroid.widget.TextView;

importandroid.widget.Toast;

publicclassStuffListextendsActivity{

privateListViewlv;

privateSQLiteDatabasemDataBase;

privateListstuffInfos;

privateMySQLiteOpenHelpermHelper;

Override

protectedvoidonCreate(BundlesavedInstanceState){

setContentView(R.layout.show_all);

mHelper=newMySQLiteOpenHelper(this);

lv=(ListView)findViewById(R.id.id_lv);

all();

super.onCreate(savedInstanceState);

}

publicvoidall(){

mDataBase=mHelper.getWritableDatabase();

Cursorcursor=mDataBase.query("LIQI",null,null,null,null,null,null);

stuffInfos=newArrayList();

while(cursor.moveToNext()){

Stuffstuff=newStuff();

stuff.setStuffName(cursor.getString

(1));

stuff.setStuffAmount(cursor.getInt

(2));

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

stuffInfos.add(stuff);

}

cursor.close();

mDataBase.close();

lv.setAdapter(newMyadapter());

}

 

classMyadapterextendsBaseAdapter{

Override

publicintgetCount(){

returnstuffInfos.size();

}

Override

publicObjectgetItem(intposition){

//TODOAuto-generatedmethodstub

returnnull;

}

Override

publiclonggetItemId(intposition){

//TODOAuto-generatedmethodstub

return0;

}

Override

publicViewgetView(finalintposition,Viewview,ViewGroupparent){

view=View.inflate(StuffList.this,R.layout.stuff_item,null);

TextViewtv_name=(TextView)view.findViewById(R.id.id_tv_name);

TextViewtv_amount=(TextView)view.findViewById(R.id.id_tv_amount);

TextViewtv_price=(TextView)view.findViewById(R.id.id_tv_price);

ImageButtoniButton=(ImageButton)view.findViewById(R.id.id_ib_delete);

tv_name.setText(stuffInfos.get(position).getStuffName());

tv_amount.setText("数量(个)"+stuffInfos.get(position).getStuffAmount());

tv_price.setText("单价(元)"+stuffInfos.get(position).getStuffPrice());

iButton.setOnClickListener(newOnClickListener(){

Override

publicvoidonClick(Viewv){

mDataBase=mHelper.getWritableDatabase();

mDataBase.delete("LIQI","stuff=?

",newString[]{stuffInfos.get(position).getStuffName()});

mDataBase.close();

stuffInfos.remove(position);

Myadapter.this.notifyDataSetChanged();

}

});

returnview;

}

}

}

6.编写界面交互代码

package.LIQI.Stuff;

importandroid.app.Activity;

importandroid.content.ContentValues;

importandroid.content.Intent;

importandroid.database.sqlite.SQLiteDatabase;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.view.View.OnClickListener;

importandroid.widget.Button;

importandroid.widget.EditText;

importandroid.widget.Toast;

publicclassMainActivityextendsActivityimplementsOnClickListener{

privateMySQLiteOpenHelpermHelper;

privateSQLiteDatabasemDataBase;

privateButtonbt_add,bt_all;

privateEditTextet_stuffName,et_stuffAmount,et_stuffPrice;

Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.add_data);

mHelper=newMySQLiteOpenHelper(this);

initView();

initAction();

}

publicvoidinitView(){

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

}

 

publicvoidinitAction(){

bt_add.setOnClickListener(this);

bt_all.setOnClickListener(this);

}

Override

publicvoidonClick(Viewv){

switch(v.getId()){

caseR.id.id_bt_add:

add();

break;

caseR.id.id_bt_all:

Intentintent=newIntent(this,StuffList.class);

startActivity(intent);

break;

}

}

publicvoida

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

当前位置:首页 > 工程科技 > 冶金矿山地质

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1