私はノートアプリケーションを作成していますが、私はほとんど完了しています。スパニング可能な文字列で問題を抱えています。/達成しようとしているのは、メモを入力してボタン私はそれが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));
}
Noteクラスはどのように見えますか?そこにテキストを文字列などで保存しますか? – Ridcully
ノートクラスとnoteAdapterを追加しました。これは文字列です。私は申し訳ありません、私は元の投稿の笑にそれらを含めることを意味しました –
最初のヒント:getView()メソッドで何度も何度も書体を作成しないでください。代わりにそれらを一度作成し(たとえば、Adapterのコンストラクタで)、メンバー変数に保持します。 – Ridcully