Android实验一实验报告

Android实验报告一

                 

                  姓名:丁军峰

班级:信科12-3

学号:08123448



一、  实验内容

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

二、    实验目的

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

三、    需求分析

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

四、    实验过程

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

 <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="0dp"

        android:layout_weight="1"

        android:orientation="vertical">

        <TextView

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="物品清单"

           

           />

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="物品名称"/>

            <EditText

                android:id="@+id/id_et_stuffname"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:inputType="text"/>

        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="物品个数"/>

            <EditText

                android:id="@+id/id_et_stuffamount"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:inputType="text"/>

        </LinearLayout>

        <LinearLayout

            android:layout_width="match_parent"

            android:layout_height="wrap_content"

            android:orientation="horizontal">

            <TextView

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="物品价格"/>

            <EditText

                android:id="@+id/id_et_stuffprize"

                android:layout_width="match_parent"

                android:layout_height="wrap_content"

                android:inputType="text"/>

        </LinearLayout>

    </LinearLayout>

         

    <FrameLayout

        android:layout_width="match_parent"

        android:layout_height="wrap_content"

       

        >

        <LinearLayout

        android:layout_width="match_parent"

        android:layout_height="match_parent"

        android:orientation="horizontal">

       

       <Button

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

      

      <Button

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

      </LinearLayout>

      <View

            android:layout_width="match_parent"

            android:layout_height="1dp"

            android:background="#eee"/>

    </FrameLayout>

 

 

   

</LinearLayout>

2.创建ListView Item布局

     <?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal"

    android:background="#ffffffff">

    <ImageView

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

    <TextView 

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

    <TextView 

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

    <TextView 

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

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

        android:onClick="deleteItem"/>

</LinearLayout>

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 MySQLiteOpenHelper extends SQLiteOpenHelper {

            public MySQLiteOpenHelper(Context context

                            ) {

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

                   // TODO Auto-generated constructor stub

         }

         @Override

         public void onCreate(SQLiteDatabase db) {

         db.execSQL("create table LIQI(_id integer primary key  autoincrement,stuff,amount,price)");

         }

         @Override

         public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

                   // TODO Auto-generated method stub

         }

}

4.创建account类

package com.LIQI.Stuff;

publicclass Stuff {

private String stuffName;

privateint  StuffAmount;

privatefloat StuffPrice;

public String getStuffName() {

    return stuffName;

}

publicvoid setStuffName(String stuffName) {

    this.stuffName = stuffName;

}

publicint getStuffAmount() {

    return StuffAmount;

}

publicvoid setStuffAmount(int stuffAmount) {

    StuffAmount = stuffAmount;

}

publicfloat getStuffPrice() {

    return StuffPrice;

}

publicvoid setStuffPrice(float stuffPrice) {

    StuffPrice = stuffPrice;

}

}

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.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 Activity{

   private ListView lv;

   private SQLiteDatabase mDataBase;

    private List<Stuff>stuffInfos;

    private MySQLiteOpenHelper mHelper;

  @Override

protected 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("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();

                     mDataBase.close();

                     lv.setAdapter( new Myadapter());

   }

  

   class Myadapter extends BaseAdapter{

            @Override

            public int getCount() {

                    

                     return stuffInfos.size();

            }

            @Override

            public Object getItem(int position) {

                     // TODO Auto-generated method stub

                     return null;

            }

            @Override

            public long getItemId(int position) {

                     // TODO Auto-generated method stub

                     return 0;

            }

            @Override

            public 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=(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(position).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=?", new String []{stuffInfos.get(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;

import 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 SQLiteDatabase mDataBase;

    private Button bt_add,bt_all;

   

    private EditText et_stuffName,et_stuffAmount,et_stuffPrice;

     @Override

     protected 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_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 initAction(){

              bt_add.setOnClickListener(this);

             

              bt_all.setOnClickListener(this);

             

     }

     @Override

     public 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.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(stuffPrice);

              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).show();

              }else {

                        et_stuffAmount.setText("");

                  et_stuffName.setText("");

                  et_stuffPrice.setText("");

                        Toast.makeText(this, "添加成功,物品条数"+number,1).show();

              }

              mDataBase.close();

     }

    

}

7.Manifest

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.LIQI.Stuff"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

         >

        <activity

            android:name=".MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

        <activity android:name="com.LIQI.Stuff.StuffList"></activity>

    </application>

</manifest>

五、 实验结果

程序界面显示

相关推荐