Android实验一实验报告_第1页
Android实验一实验报告_第2页
Android实验一实验报告_第3页
Android实验一实验报告_第4页
Android实验一实验报告_第5页
已阅读5页,还剩19页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、Android 实验报告一:丁军峰班级:信科12-3学号:08123448学习文档 仅供参考学习文档仅供参考实验内容编写一个 Android 应用程序,实现对自己物品的管理,功能包括android:layout_height="0dp"学习文档 仅供参考添加、删除和查询等实验目的了解android开发流程,掌握SQLite数据库和ListView控件的使三、需求分析使用SQLite数据库,使用ListView控件显示物品四、实验过程1. 创建程序,设计用户交互界面<? 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" >学习文档 仅供参考<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 =" 物品个数 "/><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 =" 物品价格 "/&

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"学习文档仅供参考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、创建 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”学习文档 仅供参考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. 创建数据库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. 创建 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. 创建数据操作逻辑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("数量(个)"+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. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论