Android实验一实验报告Word下载.docx
《Android实验一实验报告Word下载.docx》由会员分享,可在线阅读,更多相关《Android实验一实验报告Word下载.docx(15页珍藏版)》请在冰豆网上搜索。
![Android实验一实验报告Word下载.docx](https://file1.bdocx.com/fileroot1/2022-12/13/55e83400-261d-4a55-af19-583b7f80e8ba/55e83400-261d-4a55-af19-583b7f80e8ba1.gif)
android="
schemas.android./apk/res/android"
android:
layout_width="
match_parent"
layout_height="
orientation="
vertical"
LinearLayout
0dp"
layout_weight="
1"
TextView
wrap_content"
text="
物品清单"
/>
horizontal"
物品名称"
EditText
id="
+id/id_et_stuffname"
inputType="
text"
/LinearLayout>
物品个数"
+id/id_et_stuffamount"
物品价格"
+id/id_et_stuffprize"
FrameLayout
Button
+id/id_bt_add"
添加记录"
layout_marginLeft="
10dp"
layout_marginRight="
layout_marginTop="
6dp"
layout_marginBottom="
7dp"
gravity="
center"
padding="
5dp"
textColor="
#727272"
background="
drawable/recordbutton"
minHeight="
+id/id_bt_all"
显示全部"
View
1dp"
#eee"
/FrameLayout>
2.创建ListViewItem布局
#ffffffff"
ImageView
+id/id_ig_name"
src="
drawable/ic_menu_paste_holo_light"
+id/id_tv_name"
#000000"
textSize="
18sp"
+id/id_tv_amount"
+id/id_tv_price"
ImageButton
+id/id_ib_delete"
drawable/ic_menu_delete"
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)"
);
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.创建数据操作逻辑
importjava.util.ArrayList;
importjava.util.List;
importandroid.app.Activity;
importandroid.database.Cursor;
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;
privateList<
Stuff>
stuffInfos;
privateMySQLiteOpenHelpermHelper;
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();
}
publicObjectgetItem(intposition){
//TODOAuto-generatedmethodstub
returnnull;
publiclonggetItemId(intposition){
return0;
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("
"
stuff=?
"
newString[]{stuffInfos.get(position).getStuffName()});
mDataBase.close();
stuffInfos.remove(position);
Myadapter.this.notifyDataSetChanged();
}
});
returnview;
6.编写界面交互代码
importandroid.content.ContentValues;
importandroid.content.Intent;
importandroid.widget.Button;
importandroid.widget.EditText;
publicclassMainActivityextendsActivityimplementsOnClickListener{
privateButtonbt_add,bt_all;
privateEditTextet_stuffName,et_stuffAmount,et_stuffPrice;
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);
publicvoidonClick(Viewv){
switch(v.getId()){
caseR.id.id_bt_add:
add();
break;
caseR.id.id_bt_all:
Intentintent=newIntent(this,StuffList.class);
startActivity(intent);
publicvoida