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; }} 運行結果:() |