Android:从列表中存储数据转换为使用SQLite

时间:2013-09-28 05:42:55

标签: android sqlite

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

1 个答案:

答案 0 :(得分:1)

我会推荐摩托罗拉的eclipse插件MotoDev studio。

他们有一个专注于生成数据库表的整体视角。完成后,只需点击几下即可获得精美的ContentProviders。

ContentProviders可以轻松添加/编辑/删除行或查询数据。

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