How to store Multiline EditText into SQLiteDatabas

2019-09-15 01:46发布

问题:

I'm working on an application where I've 2 Multiline EditText and I need to store them into database. i only know how to store the single line EditText.

Here is my codes for inserting into database:

 btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View v)
            {
                //startActivityForResult(new Intent(Intent.ACTION_PICK),PICK_CONTACT);

                likeDB.open();
                long likes_id;
                Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
                String NameValue = nameSpinner.getSelectedItem().toString();

                EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
                String LikesValue = txtLikes.getText().toString();

                likes_id = likeDB.insertLikes(NameValue, LikesValue);
                likeDB.close();

                dislikeDB.open();
                long dislikes_id;
                Spinner names = (Spinner) findViewById(R.id.nameSpinner);
                String NamesValue = names.getSelectedItem().toString();

                EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);
                String DislikesValue = txtDislikes.getText().toString();

                dislikes_id = dislikeDB.insertDislikes(NamesValue, DislikesValue);

                Toast.makeText(getBaseContext(), 
                        "Your information is saved successfully!", Toast.LENGTH_SHORT).show();
                dislikeDB.close();
            }
        });

This the code where I've declared the LikeDBAdapter and DislikesDBAdapter above the onCreate() method:

LikesDBAdapter likeDB = new LikesDBAdapter(this);
DislikesDBAdapter dislikeDB = new DislikesDBAdapter(this);

I need help with this. Any help will be appreciated. Thanks!
=)

回答1:

set a method on your dbHelper class and use a ContentValues, e.g :

public boolean createNote(String VALUE_1 , String VALUE_2) {
    ContentValues initValue = new ContentValues();
    initValue.put(COLUMN_NAME_1 , VALUE_1);
    initValue.put(COLUMN_NAME_2 , VALUE_2);

    return mDb.insert(YOUR_TABLE_NAME , null, initValue) > 0;
}