2011-03-05 82 views
0

我有一個項目,我們將使用Eclipse和模擬器來製作電話簿應用程序。我已經有一個正在運行的程序添加聯繫人和查看我的聯繫人。 我的問題是刪除聯繫人,編輯聯繫人和搜索聯繫人。使用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); 
      }   
      }); 
    } 
} 

回答

0

,只需從快看看你的代碼,你可能需要一種方法來訪問已經被接觸添加,以便您可以編輯或刪除它們。由於它是一個ListView,我可能會使用setOnItemClick()來創建一個新的活動或一個包含這些信息的對話框。另外,我強烈建議將您的適配器從ArrayAdapter更改爲像SimpleCursorAdapter,它更適合訪問數據庫。在以某種方式修改數據庫的情況下,ArrayAdapter不會刷新聯繫人列表,因爲它僅在調用onCreate()時創建。

+0

謝謝大家.... – migLove 2011-03-11 03:00:48

相關問題