gpt4 book ai didi

Android:从将数据存储在列表中过渡到使用 SQLite

转载 作者:行者123 更新时间:2023-11-30 03:21:27 25 4
gpt4 key购买 nike

目前我正在使用列表来存储一堆联系人,但现在我想使用 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 数据库而不是适应 ListView 的列表?

最佳答案

我会推荐 MotoDev studio,它是摩托罗拉的一个 eclipse 插件。

他们有一个专门用于生成数据库表的整体视角。完成后,您只需单击几下即可获得生成良好的 ContentProvider。

ContentProvider 使添加/编辑/删除行或查询数据变得容易。

我会发布一些使用示例,但目前在我的手机上。

关于Android:从将数据存储在列表中过渡到使用 SQLite,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19063728/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com