making phonebook application using eclipse and emulator
I have a project which we are going to make a phonebook application using Eclipse and emulator. I already have a running program of adding contacts and viewing my contacts. my problem is the delete contacts, edit contacts, and search contacts.
Here is my 3 files:
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 DBH开发者_如何学Pythonelper;
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);
}
});
}
}
Just from a quick look at your code, you probably need a way to access contacts that have been already added so that you can edit or delete them. Since it is a ListView I would probably use setOnItemClick() to bring up a new activity or a dialog with the information. Also, I would strongly suggest changing your adapter from ArrayAdapter to something like SimpleCursorAdapter, which is better suited for accessing databases. In the event that you modify the database in some way, the ArrayAdapter is not going to refresh your list of contacts because it is only created when onCreate() is called.
精彩评论