Jordan
Jordan

Reputation: 417

Save Rich Edit Text to database

I have a edit text box (noteContent). The user has many formats they can use, here are a few:

                    if (text.equals("Bold")) {

                        //Make Spannable Text Bold//
                        spannable.setSpan(new StyleSpan(Typeface.BOLD), startSelection, endSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }



                    if (text.equals("Italic")) {

                        //Make Spannable Text Italic//
                        spannable.setSpan(new StyleSpan(Typeface.ITALIC), startSelection, endSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);



                    if (text.equals("Underline")) {

                        //Make Spannable Text Italic//
                        spannable.setSpan(new UnderlineSpan(), startSelection, endSelection, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                    }

when I save noteContent.getText.toString() To my database, then populate my listview all the rich text is gone.

How do I save the rich text in my editText to my database. So when I populate the database in my listView using a simpleCursorAdapter the rich Text is there.

Thanks.

Upvotes: 1

Views: 1185

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

You will need to convert the Spanned that getText() on EditText returns into something that you can save in a database, such as an HTML representation. Then, you can convert it back into a Spanned to show in a TextView at a later point.

One option is to use Html.toHtml() and Html.fromHtml() from the Android SDK. Another option is to use SpannedXhtmlGenerator and SpannableStringGenerator from my CWAC-RichEdit library. Or, you are welcome to come up with your own conversion system, using those implementations as a source of ideas.

Upvotes: 1

Related Questions