7

I am using commonsguy / cwac-richedit Library for Rich Text Editing. After doing so i am saving the formatted text in the database. But when i retrieve the saved formatted string its formatting is removed.

I want to know that how to save/retrieve the text from the database without losing format.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Shajeel Afzal
  • 5,913
  • 6
  • 43
  • 70

2 Answers2

10

EditText uses HTML markup (limited set). Key Interfaces for this type of markup text being Spanned and Spannable.

EditText uses Editable to represent text, which implements Spannable.

Html class is provided for conversions between markup and Spanned text, you can use it as well:

    //--suppose this is typed to an EditText called et --
    Spanned s = Html.fromHtml("<i>Hi</i> There ! <b>how're you ?</b>");
    et.setText(s);

    //--save to string--
    Editable e = et.getText();
    String s2 = Html.toHtml(e);

    //--restore from string--
    Spanned s3 = Html.fromHtml(s2);
    et.setText(s3);  
S.D.
  • 29,290
  • 3
  • 79
  • 130
  • 2
    +1, for being the best we have. Note that while `fromHtml()` and `toHtml()` are roughly aligned with what `TextView`/`EditText`/`RichEditText` can support, there are things the widgets and `Spanned` can do that `fromHtml()` does not translate and `toHtml()` does not generate. Somebody needs to create equivalents of `fromHtml()` and `toHtml()` than handle all the `CharacterStyle` variants. I'm kinda hoping that "somebody" isn't me... :-) – CommonsWare Jun 17 '13 at 10:19
  • @CommonsWare Ah I see, lots of `Span` classes are there for `CharacterStyle`. I only knew about html tags (you posted a list of those). Thanks. – S.D. Jun 17 '13 at 10:58
  • @CommonsWare i have applied every style to the text entered (i.e, `BOLD`, `ITALIC`, `UNDERLINE`, etc) all of them work fine except `STRIKETHROUGH`. I mean all of them save and load fine in the database except `STRIKETHROUGH` effect! `STRIKETHROUGH` is applied to the text but not saved in the database. – Shajeel Afzal Jun 18 '13 at 08:53
  • 1
    @ShajeelAfzal: That would suggest that `toHtml()` does not support the `strike` tag. – CommonsWare Jun 18 '13 at 10:32
  • @CommonsWare any solution for that sir? – Shajeel Afzal Jun 18 '13 at 13:33
0

Just Use: JSONField in PostgresSQL. That Miraculosly solved the issue. Yesterday, I was facing the same. Solved it via

JSONField in PostgreSQL