目前我正在使用列表来存储一堆联系人,但现在我想使用 SQLite。我读过很多教程,但它们都是从头开始制作数据库的,而且我已经有很多想要使用的代码。
这是我目前存储数据的方式。
public class NewContactActivity extends Activity {
private EditText firstName;
private EditText lastName;
private EditText mobileNumber;
private EditText homeNumber;
private EditText workNumber;
private EditText homeAddress;
private EditText emailAddress;
private EditText workAddress;
private EditText contactNotes;
private Button saveButton;
private Button cancelButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_contact);
firstName = (EditText) findViewById(R.id.firstName);
lastName = (EditText) findViewById(R.id.lastName);
mobileNumber = (EditText) findViewById(R.id.mobileNumber);
homeNumber = (EditText) findViewById(R.id.homeNumber);
workNumber = (EditText) findViewById(R.id.workNumber);
homeAddress = (EditText) findViewById(R.id.homeAddress);
emailAddress = (EditText) findViewById(R.id.emailAddress);
workAddress = (EditText) findViewById(R.id.workAddress);
contactNotes = (EditText) findViewById(R.id.contactNotes);
saveButton = (Button) findViewById(R.id.saveButton);
cancelButton = (Button) findViewById(R.id.cancelButton);
// set listener for the save button so that when it is pressed
// a contact added and the home screen becomes the view.
saveButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Contact.contactList//<<<<This is a static arrayList in the contact class that is having contacts added to it
.add(new Contact(firstName.getText().toString(),
lastName.getText().toString(), mobileNumber
.getText().toString(), homeNumber
.getText().toString(), workNumber
.getText().toString(), homeAddress
.getText().toString(), emailAddress
.getText().toString(), workAddress
.getText().toString(), contactNotes
.getText().toString()));
HomeScreenActivity.selectedContactIndex = -1;
Intent intent = new Intent(v.getContext(),
HomeScreenActivity.class);
v.getContext().startActivity(intent);
}
});
// the cancel button will just return the user to the home screen
cancelButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(v.getContext(),
HomeScreenActivity.class);
v.getContext().startActivity(intent);
}
});
}
如何使用此代码添加到 sqlite 数据库而不是适用于列表视图的列表?