實驗8 SQLite資料庫操作

來源:互聯網
上載者:User

標籤:

實驗報告

課程名稱

基於Android平台移動互連網開發

實驗日期

    2016/5/6

實驗項目名稱

SQLite資料庫操作

實驗地點

綜合實驗室

實驗類型

□驗證型    √設計型    □綜合型

學  時

           2

一、實驗目的及要求(本實驗所涉及並要求掌握的知識點)

1. 設計一個個人通訊錄,掌握Android平台下的資料庫開發,該個人通訊錄主要包括連絡人清單和連絡人詳細資料等介面。

2. 程式主介面是通訊錄的目錄顯示手機上連絡人的名稱。點擊連絡人的姓名可以顯示連絡人的詳細資料。在按了MEMU鍵之後會快顯功能表欄。單擊功能表列上的按鈕可以新增連絡人...和刪除連絡人。

二、實驗環境(本實驗所使用的硬體裝置和相關軟體)

(1)PC機

(2)作業系統:Windows XP

(3)軟體: Eclipse, JDK1.6,Android SDK,ADT

三、實驗內容及步驟

1)確定資料庫的資料結構

2)在res/drawable-mdpi目錄下拷入程式要用的表徵圖

3)定義字串資源string.xml

4)開發布局檔案activity_main.xml用於顯示連絡人清單

5)layout目錄下建立一個detail.xml,用於顯示連絡人詳細資料

6)開發資料庫輔助類MyOpenHelper類,建立一個MyOpenHelper.java

7)接下來便進入MainActivity端的開發,實現資料庫增加、刪除、修改記錄等操作

8)建立一個Activity名字叫DetailActivity.java,實現連絡人詳細資料顯示功能

四、實驗結果(本實驗來源程式清單及運行結果或實驗結論、實驗設計圖)

代碼:

MainActivity部分代碼:

package com.example.tongxunlu;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.database.Cursor;import android.support.v4.widget.CursorAdapter;import android.support.v4.widget.SimpleCursorAdapter;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.AdapterView;import android.widget.AdapterView.OnItemClickListener;import android.widget.Button;import android.widget.ListView;public class MainActivity extends Activity {    Button btn1,btn2;    ListView listview;    public static MyOpenHelper dbhelper;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn1=(Button)findViewById(R.id.button1);        btn2=(Button)findViewById(R.id.button2);        listview=(ListView)findViewById(R.id.lv);        dbhelper=new MyOpenHelper(MainActivity.this, "myDict.db10", 1);        Cursor cursor=dbhelper.getReadableDatabase().rawQuery("select * from contacts", null);        inflateList(cursor);        btn1.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                Intent intent=new Intent();                intent.setClass(MainActivity.this, BaocunActivity.class);                startActivity(intent);                MainActivity.this.finish();            }        });               btn2.setOnClickListener(new OnClickListener() {                        @Override            public void onClick(View arg0) {                Intent intent=new Intent();                intent.setClass(MainActivity.this, shanchuActivity.class);                startActivity(intent);                MainActivity.this.finish();            }        });                    }     private void inflateList(Cursor cursor) {            SimpleCursorAdapter simpleCursorAdapter=new SimpleCursorAdapter(MainActivity.this,                    R.layout.lian, cursor, new String[]{"name","phone"},                     new int[]{R.id.text1,R.id.text2}, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);            listview.setAdapter(simpleCursorAdapter);            listview.setOnItemClickListener(new ItemClickListener());         }     private final class ItemClickListener implements OnItemClickListener{                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                 ListView listview = (ListView) parent;                 Cursor cursor1 = (Cursor) listview.getItemAtPosition(position);                String personid = cursor1.getString(cursor1.getColumnIndex("name"));                String personid1 = cursor1.getString(cursor1.getColumnIndex("phone"));                 String personid2 = cursor1.getString(cursor1.getColumnIndex("mobile"));                String personid3 = cursor1.getString(cursor1.getColumnIndex("email"));                String personid4 = cursor1.getString(cursor1.getColumnIndex("post"));                String personid5 = cursor1.getString(cursor1.getColumnIndex("addr"));                String personid6 = cursor1.getString(cursor1.getColumnIndex("comp"));                Intent intent=new Intent();                intent.putExtra("username1",personid);                 intent.putExtra("userphone1",personid1);                intent.putExtra("usermobile1",personid2);                intent.putExtra("useremail1",personid3);                intent.putExtra("userpost1",personid4);                intent.putExtra("useraddr1",personid5);                intent.putExtra("usercomp1",personid6);                intent.setClass(MainActivity.this, DetailActivity.class);                startActivity(intent);                MainActivity.this.finish();                              }          }       @Override    public boolean onCreateOptionsMenu(Menu menu) {        // Inflate the menu; this adds items to the action bar if it is present.        getMenuInflater().inflate(R.menu.main, menu);        return true;    }}

 

 

運行結果:()

 

 

五、實驗總結(對本實驗結果進行分析,實驗心得體會及改進意見)

    這次實驗感覺挺難的,做起來卻不盡人意,多次出現崩潰,又沒出現錯誤,挺難找出問題來。

實驗評語

 

實驗成績

 

指導教師簽名:                                               年   月   日

實驗8 SQLite資料庫操作

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.