2017-11-07 4 views
1

私はノートアプリケーションを作成していますが、私はほとんど完了しています。スパニング可能な文字列で問題を抱えています。/達成しようとしているのは、メモを入力してボタン私はそれがspannable文字列の助けを借りて達成されているedittextフィールドに大きな、太字の文字列を追加したい。私はメモを保存して終了し、メモに戻って、spannable文字列はまだそこにありますが、もはや大きく太字ではありません:/ spannable文字列を保存する方法はありますか?もしそうでなければ、同じ効果を得る方法はありますか?またサイドノート:spearable文字列のテキストを1つのオブジェクトにする方法があるかどうか疑問に思っていました。だから、私はそれを消去すると、すべて一緒に消去されます。普通のテキストのようなそれぞれの文字ではありません。高度でのおかげで、私は本当に任意の洞察力に感謝:Dスパニング可能な文字列を保存しますか?

はHERESにここに私のNoteActivityでボタン

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
     case R.id.save_note: 
      save_note(); 
      return true; 
     case android.R.id.home: 
      save_note(); 
      finish(); 
      return true; 

     case R.id.add_intro: 
      SpannableString intro; 
      if (note_content.length() == 0) { 
       intro = new SpannableString("(Intro:)\n"); 
       intro.setSpan(new StyleSpan(Typeface.BOLD), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
       intro.setSpan(new RelativeSizeSpan(1.25f), 0, 8, 0); 
      } else { 
       intro = new SpannableString("\n\n(Intro:)\n"); 
       intro.setSpan(new StyleSpan(Typeface.BOLD), 0, 11, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
       intro.setSpan(new RelativeSizeSpan(1.25f), 0, 11, 0); 
      } 
      note_content.append(intro); 
      return true; 

とは、ノートが保存され、 ユーティリティ

public class Utilities { 

public static final String FileExtention = ".bin"; 

public static boolean save_note(Context context, Note note) { 

    String fileName = String.valueOf(note.getDateTime()) + FileExtention; 

    FileOutputStream fos; 
    ObjectOutputStream oos; 

    try { 
     fos = context.openFileOutput(fileName, context.MODE_PRIVATE); 
     oos = new ObjectOutputStream(fos); 
     oos.writeObject(note); 
     oos.close(); 
     fos.close(); 

    } catch (IOException e) { 
     e.printStackTrace(); 
     return false; 

    } 


    return true; 
} 

public static ArrayList<Note> getAllSavedNotes(Context context) { 
    ArrayList<Note> notes = new ArrayList<>(); 

    File filesDir = context.getFilesDir(); 
    ArrayList<String> noteFiles = new ArrayList(); 

    for(String file : filesDir.list()) { 
     if(file.endsWith(FileExtention)) { 
      noteFiles.add(file); 
     } 
    } 

    FileInputStream fis; 
    ObjectInputStream ois; 

    for(int i = 0; i < noteFiles.size(); i++) { 
     try{ 
      fis = context.openFileInput(noteFiles.get(i)); 
      ois = new ObjectInputStream(fis); 

      notes.add((Note)ois.readObject()); 

      fis.close(); 
      ois.close(); 

     } catch (IOException | ClassNotFoundException e) { 
      e.printStackTrace(); 
      return null; 
     } 
    } 

    return notes; 
} 

HERESにNoteAdapter

をロードされている方法です
public class NoteAdapter extends ArrayAdapter<Note> { 

public NoteAdapter(Context context, int resource, ArrayList<Note> notes) { 
    super(context, resource, notes); 


} 

@Override 
public View getView(int position, View convertView, @NonNull ViewGroup parent) { 
    //return super.getView(position, convertView, parent); 

    if(convertView == null) { 
     convertView = LayoutInflater.from(getContext()) 
       .inflate(R.layout.item_note, null); 
    } 

    Note note = getItem(position); 

    if(note != null) { 
     TextView title = (TextView) convertView.findViewById(R.id.list_note_title); 
     TextView content = (TextView) convertView.findViewById(R.id.list_note_content); 
     TextView date = (TextView) convertView.findViewById(R.id.list_note_date); 

     Typeface music_font = Typeface.createFromAsset(getContext().getAssets(), "fonts/melodymakernotesonly.ttf"); 
     Typeface scribble_card = Typeface.createFromAsset(getContext().getAssets(), "fonts/the unseen.ttf"); 
     title.setTypeface(music_font); 
     content.setTypeface(scribble_card); 

     title.setText(note.getTitle()); 
     date.setText(note.getDateTimeFormatted(getContext())); 

     if(note.getContent().length() > 25) { 
      content.setText(note.getContent().substring(0,25) + "..."); 
     } else { 
      content.setText(note.getContent()); 
     } 

     if(note.getContent().length() <= 0) { 
      content.setText("(Empty Note..)"); 
     } else { 
      content.setText(note.getContent()); 
     } 

     if (note.getTitle().length() <= 0) { 
      title.setText("(Untitled)"); 
     } else { 
      title.setText(note.getTitle()); 
     } 
    } 

    return convertView; 
} 

ここではマイノートクラス

public class Note implements Serializable { 

private long mDateTime; 
private String mTitle; 
private String mContent; 

public Note(long dateTime, String title, String content) { 
    mContent = content; 
    mTitle = title; 
    mDateTime = dateTime; 
} 

public void setDateTime(long dateTime) { 
    mDateTime = dateTime; 
} 

public void setTitle(String title) { 
    mTitle = title; 
} 

public void setContent(String content) { 
    mContent = content; 
} 

public long getDateTime() { 
    return mDateTime; 
} 

public String getTitle() { 
    return mTitle; 
} 

public String getContent() { 
    return mContent; 
} 

public String getDateTimeFormatted(Context context) { 
    SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:SS", context.getResources().getConfiguration().locale); 
    sdf.setTimeZone(TimeZone.getDefault()); 
    return sdf.format(new Date(mDateTime)); 
} 

+0

Noteクラスはどのように見えますか?そこにテキストを文字列などで保存しますか? – Ridcully

+0

ノートクラスとnoteAdapterを追加しました。これは文字列です。私は申し訳ありません、私は元の投稿の笑にそれらを含めることを意味しました –

+0

最初のヒント:getView()メソッドで何度も何度も書体を作成しないでください。代わりにそれらを一度作成し(たとえば、Adapterのコンストラクタで)、メンバー変数に保持します。 – Ridcully

答えて

1

SpannableStringを通常StringNote.mContentなど)に変換すると、スパンの情報が失われます。 SpannableStringで使用されるスパンはHTMLタグのようなものではありません。それらを実際の文字列の追加情報と考えることができます。こんにちは、私は大胆文字列だ

は、この文字列を取ることができます。 HTMLで

Hi, I am a **bold** string.

それはそうのようになります:SpannableStringがあるとして、しかし

Hi, I am a <strong>bold</strong> string.

SOこれがそうのように見える上、ここで使用さ値下げで

次のような構造になります。

Text : Hi, I am a bold string. Span : Render characters [11..14] in bold

実際のテキストと追加情報(太字で表示するテキストの部分)があります。

あなたは、その情報を保存したい場合は、Androidが提供して"Hi, I am a <b>bold</b> string."のような文字列を使用するか、テキストの内容に加えて、フォーマット情報を保存する必要があり、基本的なHTMLのサポートを使用することができます。

EDIT

はHTMLで文字列を使用するには、あなたがHtml.fromHtml()使用する必要があります:あなたは書式設定情報を保存するために、あなたのNoteクラスを拡張する必要があるだろうということ、

textView.setText(Html.fromHtml(getString(R.string.your_html_string))); 

保存形式情報を意味し、基本的にはあなたが文字列に適用した範囲です。あなたは「、

class FormatInfo { 
    int from; 
    int to; 
    int formatCode; // <-- e.g. 1 = bold, 2 = italic etc. 
} 

class FormattedNote extends Note { 
    List<FormatInfo> formatInfos = new ArrayList<>(); 

} 

そして、あなたはFormatInfoオブジェクトに適用スパンを変換してノートを保存するときにして、再度SpannableStringを作成するときにFormattedNoteにそれらを追加する必要があると思います。たとえば、あなたがこのようなものを使用することができますノートのformatInfosからスパンを作成する必要があります。

+0

さて、それは感覚の多くを作る、すべてのヒントと助けてくれてありがとう!本当に感謝しています。だから、太字のテキストを文字列リソースファイルに入れようとしました。(intro :)これはあなたが意図したもので、getString(R.string.intro);残念ながら、太字はEditTextに追加されたときに表示されません:/あなたは、テキストコンテンツにフォーマット情報を保存してどういう意味ですか?私は私がそれを試すことができたと思う、他のすべてが再び失敗したように思われる、私は本当にすべての助けに感謝! –

+0

あなたの質問に答えるために私の答えが更新されました。 Btw。私はPlaystore(Memorix)にメモアプリを持っていて、それはとても複雑なので、フォーマットオプションを一切追加していませんでした:-) – Ridcully

+0

Ohkay、私は文字列を保存するようになっていませんでしたが、私はノートを開くたびに文字列を再作成して置き換える置換機能を見つけました。書式設定情報の保存に関するリンクや情報がありますか?最終的にそれらを救うことができるようにidとして。すべての助けをありがとう:) –

関連する問題