使用eclipse和模拟器制作电话簿应用程序

时间:2011-03-05 02:02:30

标签: android

我有一个项目,我们将使用Eclipse和模拟器制作电话簿应用程序。我已经有一个添加联系人和查看我的联系人的运行程序。 我的问题是删除联系人,编辑联系人和搜索联系人。

这是我的3个文件:

package jovi.phonebook;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

// ContactDb class that encapsulates the database functions of this application
public class ContactDB 
{

    // define constants
    public static final String KEY_ROWID = "ContactID";
    public static final String KEY_FirstName= "FirstName";
    public static final String KEY_LastName = "LastName";
    public static final String KEY_Nickname = "Nickname";
    public static final String KEY_Mobile01 = "Mobile01";
    private static final String TAG = "DBAdapter";

    private static final String DATABASE_NAME = "PhonebookDB";
    private static final String DATABASE_TABLE = "Contacts";
    private static final int DATABASE_VERSION = 1;

    private static final String DATABASE_CREATE = "create table Contacts (ContactID integer primary key autoincrement, FirstName text not null, LastName text not null, Nickname text not null, Mobile01 text not null);";

    private final Context context;  

    private DatabaseHelper DBHelper;
    private SQLiteDatabase db;

    public ContactDB(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private static class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        // creates the db if it does not exist
        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            db.execSQL(DATABASE_CREATE);
        }

        // called when the db needs upgrading
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
                              int newVersion) 
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS titles");
            onCreate(db);
        }
    }

  //open db
    public ContactDB open() throws SQLException 
    {
        db = DBHelper.getWritableDatabase();
        return this;
    }

    //close db
    public void close() 
    {
        DBHelper.close();
    }

    //add a record
    public long insertContacts(String FirstName, String LastName, String Nickname, String Mobile01) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_FirstName, FirstName);
        initialValues.put(KEY_LastName, LastName);
        initialValues.put(KEY_Nickname, Nickname);
        initialValues.put(KEY_Mobile01, Mobile01);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }

    //delete a record
    public boolean deleteContacts(long rowId) 
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    //get all record
    public Cursor getAllContacts() 
    {
        return db.query(DATABASE_TABLE, new String[] {KEY_ROWID,KEY_Nickname}, null, null, null, null, null);
    }

    //get a record
    public Cursor getContacts(long rowId) throws SQLException 
    {
        Cursor mCursor =
                db.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,KEY_FirstName,KEY_LastName, KEY_Nickname,KEY_Mobile01}, 
                        KEY_ROWID + "=" + rowId, 
                        null, null, null, null, null);

        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;
    }

    //record update
    public boolean updateContacts(long rowId, String FirstName, String LastName, String Nickname, String Mobile01) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_FirstName, FirstName);
        args.put(KEY_LastName, LastName);
        args.put(KEY_Nickname, Nickname);
        args.put(KEY_Mobile01, Mobile01);
        return db.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }
} 

package jovi.phonebook;

import android.app.Activity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.EditText;
import android.view.View;
import java.lang.String;
import android.content.Intent;

public class CreateContact extends Activity
{

    //create button widgets
    private Button SaveButton;
    private Button CancelButton;
    private EditText FirstName;
    private EditText LastName;
    private EditText Nickname;
    private EditText Mobile01;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.createcontact);

        final ContactDB PhonebookDB = new ContactDB(this);

        // reference widgets
        SaveButton = (Button)this.findViewById(R.id.ButtonSave);
        CancelButton = (Button)this.findViewById(R.id.ButtonCancel);
        FirstName = (EditText)this.findViewById(R.id.EditFirstName);
        LastName = (EditText)this.findViewById(R.id.EditLastName);
        Nickname = (EditText)this.findViewById(R.id.EditNickname);
        Mobile01 = (EditText)this.findViewById(R.id.EditMobileNumber);

        // create listener for the button
        SaveButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // put code here to execute when button is clicked
                long intNewID = 0;

                String strFirstName = FirstName.getText().toString();
                String strLastName = LastName.getText().toString();
                String strNickname = Nickname.getText().toString();
                String strMobile01 = Mobile01.getText().toString();

                PhonebookDB.open();
                intNewID = PhonebookDB.insertContacts(strFirstName, strLastName,strNickname, strMobile01);
                PhonebookDB.close();

                Intent IntentBack = new Intent();
                setResult(RESULT_OK, IntentBack);
                finish();
            }
          });

        // create listener for the button
        CancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // put code here to execute when button is clicked

                finish();
            }
          });
    }
}

package jovi.phonebook;

import android.app.Activity;
import android.os.Bundle;

import android.widget.Button;
import android.view.View;
import android.content.Intent;
import android.widget.ListView;
import android.widget.ArrayAdapter;
import android.database.Cursor;
import java.lang.String;
import java.util.ArrayList;
import java.util.List;

public class ListContact extends Activity
{

    private Button CreateButton;
    private Cursor CursorList;
    private ListView ContactsListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        CreateButton = (Button)this.findViewById(R.id.ButtonCreate);
        ContactsListView = (ListView)this.findViewById(R.id.ListView01);
        List<String> ListContact = new ArrayList<String>();

        ContactDB PhonebookDB = new ContactDB(this);
        PhonebookDB.open();
        CursorList = PhonebookDB.getAllContacts();

        if (CursorList.moveToFirst()) 
        {
            do
            {
                ListContact.add(CursorList.getString(1).toString());
            }while (CursorList.moveToNext());
        }

        ContactsListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ListContact));  

        PhonebookDB.close();

        CreateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v)
            {
                // put code here to execute when button is clicked
                Intent IntentCreateContact = new Intent(v.getContext(),CreateContact.class);
                startActivityForResult(IntentCreateContact, 0);
             }          
          });
    }
}

1 个答案:

答案 0 :(得分:0)

只需快速查看代码,您可能需要一种方法来访问已添加的联系人,以便您可以编辑或删除它们。由于它是一个ListView,我可能会使用setOnItemClick()来显示一个新的活动或带有信息的对话框。另外,我强烈建议将适配器从ArrayAdapter更改为SimpleCursorAdapter,它更适合访问数据库。如果您以某种方式修改数据库,ArrayAdapter将不会刷新您的联系人列表,因为它仅在调用onCreate()时创建。