Skip to content

Commit e556234

Browse files
committed
2018.11.16
修复Demo中的各种异常
1 parent 932897b commit e556234

File tree

5 files changed

+122
-134
lines changed

5 files changed

+122
-134
lines changed

app/src/main/java/com/sendtion/xrichtextdemo/ui/MainActivity.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,10 @@ private void initView() {
5050
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_main);
5151
setSupportActionBar(toolbar);
5252

53-
final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_main);
54-
fab.setOnClickListener(new View.OnClickListener() {
55-
@Override
56-
public void onClick(View view) {
57-
Snackbar.make(view, "新建笔记", Snackbar.LENGTH_LONG)
58-
.setAction("Action", null).show();
59-
Intent intent = new Intent(MainActivity.this, NewActivity.class);
60-
intent.putExtra("groupName", groupName);
61-
intent.putExtra("flag", 0);
62-
startActivity(intent);
63-
}
64-
});
65-
6653
noteDao = new NoteDao(this);
6754

6855
rv_list_main = (RecyclerView) findViewById(R.id.rv_list_main);
69-
7056
rv_list_main.addItemDecoration(new SpacesItemDecoration(0));//设置item间距
71-
7257
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
7358
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);//竖向列表
7459
rv_list_main.setLayoutManager(layoutManager);
@@ -116,6 +101,8 @@ public void onClick(DialogInterface dialog, int which) {
116101

117102
//刷新笔记列表
118103
private void refreshNoteList(){
104+
if (noteDao == null)
105+
noteDao = new NoteDao(this);
119106
noteList = noteDao.queryNotesAll(groupId);
120107
mNoteListAdapter.setmNotes(noteList);
121108
mNoteListAdapter.notifyDataSetChanged();

app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java

Lines changed: 88 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ protected void onCreate(Bundle savedInstanceState) {
8787
private void initView() {
8888
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_new);
8989
setSupportActionBar(toolbar);
90-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
90+
if (getSupportActionBar() != null) {
91+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
92+
}
9193
//toolbar.setNavigationIcon(R.drawable.ic_dialog_info);
9294
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
9395
@Override
@@ -96,15 +98,6 @@ public void onClick(View v) {
9698
}
9799
});
98100

99-
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab_new);
100-
fab.setOnClickListener(new View.OnClickListener() {
101-
@Override
102-
public void onClick(View view) {
103-
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
104-
.setAction("Action", null).show();
105-
}
106-
});
107-
108101
groupDao = new GroupDao(this);
109102
noteDao = new NoteDao(this);
110103
note = new Note();
@@ -121,34 +114,6 @@ public void onClick(View view) {
121114
tv_new_time = (TextView) findViewById(R.id.tv_new_time);
122115
tv_new_group = (TextView) findViewById(R.id.tv_new_group);
123116

124-
// 图片删除事件
125-
et_new_content.setOnRtImageDeleteListener(new RichTextEditor.OnRtImageDeleteListener() {
126-
127-
@Override
128-
public void onRtImageDelete(String imagePath) {
129-
if (!TextUtils.isEmpty(imagePath)) {
130-
boolean isOK = SDCardUtil.deleteFile(imagePath);
131-
if (isOK) {
132-
showToast("删除成功:" + imagePath);
133-
}
134-
}
135-
}
136-
});
137-
// 图片点击事件
138-
et_new_content.setOnRtImageClickListener(new RichTextEditor.OnRtImageClickListener() {
139-
@Override
140-
public void onRtImageClick(String imagePath) {
141-
myContent = getEditData();
142-
if (!TextUtils.isEmpty(myContent)){
143-
List<String> imageList = StringUtils.getTextFromHtml(myContent, true);
144-
if (!TextUtils.isEmpty(imagePath)) {
145-
int currentPosition = imageList.indexOf(imagePath);
146-
showToast("点击图片:" + currentPosition + ":" + imagePath);
147-
}
148-
}
149-
}
150-
});
151-
152117
openSoftKeyInput();//打开软键盘显示
153118

154119
Intent intent = getIntent();
@@ -178,9 +143,7 @@ public void onRtImageClick(String imagePath) {
178143
et_new_content.post(new Runnable() {
179144
@Override
180145
public void run() {
181-
//showEditData(note.getContent());
182-
et_new_content.clearAllLayout();
183-
showDataSync(note.getContent());
146+
dealWithContent();
184147
}
185148
});
186149
}
@@ -196,6 +159,40 @@ public void run() {
196159

197160
}
198161

162+
private void dealWithContent(){
163+
//showEditData(note.getContent());
164+
et_new_content.clearAllLayout();
165+
showDataSync(note.getContent());
166+
167+
// 图片删除事件
168+
et_new_content.setOnRtImageDeleteListener(new RichTextEditor.OnRtImageDeleteListener() {
169+
170+
@Override
171+
public void onRtImageDelete(String imagePath) {
172+
if (!TextUtils.isEmpty(imagePath)) {
173+
boolean isOK = SDCardUtil.deleteFile(imagePath);
174+
if (isOK) {
175+
showToast("删除成功:" + imagePath);
176+
}
177+
}
178+
}
179+
});
180+
// 图片点击事件
181+
et_new_content.setOnRtImageClickListener(new RichTextEditor.OnRtImageClickListener() {
182+
@Override
183+
public void onRtImageClick(String imagePath) {
184+
myContent = getEditData();
185+
if (!TextUtils.isEmpty(myContent)){
186+
List<String> imageList = StringUtils.getTextFromHtml(myContent, true);
187+
if (!TextUtils.isEmpty(imagePath)) {
188+
int currentPosition = imageList.indexOf(imagePath);
189+
showToast("点击图片:" + currentPosition + ":" + imagePath);
190+
}
191+
}
192+
}
193+
});
194+
}
195+
199196
/**
200197
* 关闭软键盘
201198
*/
@@ -247,8 +244,10 @@ public void onComplete() {
247244
if (loadingDialog != null){
248245
loadingDialog.dismiss();
249246
}
250-
//在图片全部插入完毕后,再插入一个EditText,防止最后一张图片后无法插入文字
251-
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
247+
if (et_new_content != null) {
248+
//在图片全部插入完毕后,再插入一个EditText,防止最后一张图片后无法插入文字
249+
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
250+
}
252251
}
253252

254253
@Override
@@ -266,14 +265,16 @@ public void onSubscribe(Disposable d) {
266265

267266
@Override
268267
public void onNext(String text) {
269-
if (text.contains("<img") && text.contains("src=")) {
270-
//imagePath可能是本地路径,也可能是网络地址
271-
String imagePath = StringUtils.getImgSrc(text);
272-
//插入空的EditText,以便在图片前后插入文字
273-
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
274-
et_new_content.addImageViewAtIndex(et_new_content.getLastIndex(), imagePath);
275-
} else {
276-
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), text);
268+
if (et_new_content != null) {
269+
if (text.contains("<img") && text.contains("src=")) {
270+
//imagePath可能是本地路径,也可能是网络地址
271+
String imagePath = StringUtils.getImgSrc(text);
272+
//插入空的EditText,以便在图片前后插入文字
273+
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), "");
274+
et_new_content.addImageViewAtIndex(et_new_content.getLastIndex(), imagePath);
275+
} else {
276+
et_new_content.addEditTextAtIndex(et_new_content.getLastIndex(), text);
277+
}
277278
}
278279
}
279280
});
@@ -315,7 +316,7 @@ private String getEditData() {
315316
/**
316317
* 保存数据,=0销毁当前界面,=1不销毁界面,为了防止在后台时保存笔记并销毁,应该只保存笔记
317318
*/
318-
private void saveNoteData(boolean isBackground) {
319+
private void saveNoteData(boolean isBackground) throws Exception {
319320
String noteTitle = et_new_title.getText().toString();
320321
String noteContent = getEditData();
321322
String groupName = tv_new_group.getText().toString();
@@ -382,7 +383,11 @@ public boolean onOptionsItemSelected(MenuItem item) {
382383
callGallery();
383384
break;
384385
case R.id.action_new_save:
385-
saveNoteData(false);
386+
try {
387+
saveNoteData(false);
388+
} catch (Exception e) {
389+
e.printStackTrace();
390+
}
386391
break;
387392
}
388393
return super.onOptionsItemSelected(item);
@@ -502,37 +507,45 @@ protected void onResume() {
502507
@Override
503508
protected void onStop() {
504509
super.onStop();
505-
//如果APP处于后台,或者手机锁屏,则保存数据
506-
if (CommonUtil.isAppOnBackground(getApplicationContext()) ||
507-
CommonUtil.isLockScreeen(getApplicationContext())){
508-
saveNoteData(true);//处于后台时保存数据
509-
}
510+
try {
511+
//如果APP处于后台,或者手机锁屏,则保存数据
512+
if (CommonUtil.isAppOnBackground(getApplicationContext()) ||
513+
CommonUtil.isLockScreeen(getApplicationContext())){
514+
saveNoteData(true);//处于后台时保存数据
515+
}
510516

511-
if (subsLoading != null && subsLoading.isDisposed()){
512-
subsLoading.dispose();
513-
}
514-
if (subsInsert != null && subsInsert.isDisposed()){
515-
subsInsert.dispose();
517+
if (subsLoading != null && subsLoading.isDisposed()){
518+
subsLoading.dispose();
519+
}
520+
if (subsInsert != null && subsInsert.isDisposed()){
521+
subsInsert.dispose();
522+
}
523+
} catch (Exception e) {
524+
e.printStackTrace();
516525
}
517526
}
518527

519528
/**
520529
* 退出处理
521530
*/
522531
private void dealwithExit(){
523-
String noteTitle = et_new_title.getText().toString();
524-
String noteContent = getEditData();
525-
String groupName = tv_new_group.getText().toString();
526-
String noteTime = tv_new_time.getText().toString();
527-
if (flag == 0) {//新建笔记
528-
if (noteTitle.length() > 0 || noteContent.length() > 0) {
529-
saveNoteData(false);
530-
}
531-
}else if (flag == 1) {//编辑笔记
532-
if (!noteTitle.equals(myTitle) || !noteContent.equals(myContent)
533-
|| !groupName.equals(myGroupName) || !noteTime.equals(myNoteTime)) {
534-
saveNoteData(false);
532+
try {
533+
String noteTitle = et_new_title.getText().toString();
534+
String noteContent = getEditData();
535+
String groupName = tv_new_group.getText().toString();
536+
String noteTime = tv_new_time.getText().toString();
537+
if (flag == 0) {//新建笔记
538+
if (noteTitle.length() > 0 || noteContent.length() > 0) {
539+
saveNoteData(false);
540+
}
541+
}else if (flag == 1) {//编辑笔记
542+
if (!noteTitle.equals(myTitle) || !noteContent.equals(myContent)
543+
|| !groupName.equals(myGroupName) || !noteTime.equals(myNoteTime)) {
544+
saveNoteData(false);
545+
}
535546
}
547+
} catch (Exception e) {
548+
e.printStackTrace();
536549
}
537550
finish();
538551
}

app/src/main/java/com/sendtion/xrichtextdemo/ui/NoteActivity.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -102,23 +102,6 @@ public void onClick(View view) {
102102
tv_note_time = (TextView) findViewById(R.id.tv_note_time);
103103
tv_note_group = (TextView) findViewById(R.id.tv_note_group);
104104

105-
// 图片点击事件
106-
tv_note_content.setOnRtImageClickListener(new RichTextView.OnRtImageClickListener() {
107-
@Override
108-
public void onRtImageClick(String imagePath) {
109-
ArrayList<String> imageList = StringUtils.getTextFromHtml(myContent, true);
110-
int currentPosition = imageList.indexOf(imagePath);
111-
showToast("点击图片:"+currentPosition+":"+imagePath);
112-
113-
//点击图片预览
114-
// PhotoPreview.builder()
115-
// .setPhotos(imageList)
116-
// .setCurrentItem(currentPosition)
117-
// .setShowDeleteButton(false)
118-
// .start(NoteActivity.this);
119-
}
120-
});
121-
122105
Intent intent = getIntent();
123106
Bundle bundle = intent.getBundleExtra("data");
124107
note = (Note) bundle.getSerializable("note");
@@ -136,16 +119,37 @@ public void onRtImageClick(String imagePath) {
136119
tv_note_content.post(new Runnable() {
137120
@Override
138121
public void run() {
139-
//showEditData(myContent);
140-
tv_note_content.clearAllLayout();
141-
showDataSync(myContent);
122+
dealWithContent();
142123
}
143124
});
144125
tv_note_time.setText(note.getCreateTime());
145126
}
146127

147128
}
148129

130+
private void dealWithContent(){
131+
//showEditData(myContent);
132+
tv_note_content.clearAllLayout();
133+
showDataSync(myContent);
134+
135+
// 图片点击事件
136+
tv_note_content.setOnRtImageClickListener(new RichTextView.OnRtImageClickListener() {
137+
@Override
138+
public void onRtImageClick(String imagePath) {
139+
ArrayList<String> imageList = StringUtils.getTextFromHtml(myContent, true);
140+
int currentPosition = imageList.indexOf(imagePath);
141+
showToast("点击图片:"+currentPosition+":"+imagePath);
142+
143+
//点击图片预览
144+
// PhotoPreview.builder()
145+
// .setPhotos(imageList)
146+
// .setCurrentItem(currentPosition)
147+
// .setShowDeleteButton(false)
148+
// .start(NoteActivity.this);
149+
}
150+
});
151+
}
152+
149153
/**
150154
* 异步方式显示数据
151155
* @param html
@@ -185,12 +189,14 @@ public void onSubscribe(Disposable d) {
185189

186190
@Override
187191
public void onNext(String text) {
188-
if (text.contains("<img") && text.contains("src=")) {
189-
//imagePath可能是本地路径,也可能是网络地址
190-
String imagePath = StringUtils.getImgSrc(text);
191-
tv_note_content.addImageViewAtIndex(tv_note_content.getLastIndex(), imagePath);
192-
} else {
193-
tv_note_content.addTextViewAtIndex(tv_note_content.getLastIndex(), text);
192+
if (tv_note_content !=null) {
193+
if (text.contains("<img") && text.contains("src=")) {
194+
//imagePath可能是本地路径,也可能是网络地址
195+
String imagePath = StringUtils.getImgSrc(text);
196+
tv_note_content.addImageViewAtIndex(tv_note_content.getLastIndex(), imagePath);
197+
} else {
198+
tv_note_content.addTextViewAtIndex(tv_note_content.getLastIndex(), text);
199+
}
194200
}
195201
}
196202
});

app/src/main/res/layout/activity_main.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,4 @@
2323

2424
<include layout="@layout/content_main" />
2525

26-
<android.support.design.widget.FloatingActionButton
27-
android:id="@+id/fab_main"
28-
android:layout_width="wrap_content"
29-
android:layout_height="wrap_content"
30-
android:layout_gravity="bottom|end"
31-
android:layout_margin="@dimen/fab_margin"
32-
android:visibility="gone"
33-
app:srcCompat="@android:drawable/ic_dialog_email" />
34-
3526
</android.support.design.widget.CoordinatorLayout>

app/src/main/res/layout/activity_new.xml

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,4 @@
2323

2424
<include layout="@layout/content_new" />
2525

26-
<android.support.design.widget.FloatingActionButton
27-
android:id="@+id/fab_new"
28-
android:layout_width="wrap_content"
29-
android:layout_height="wrap_content"
30-
android:layout_gravity="bottom|end"
31-
android:layout_margin="@dimen/fab_margin"
32-
android:visibility="gone"
33-
app:srcCompat="@android:drawable/ic_dialog_email" />
34-
3526
</android.support.design.widget.CoordinatorLayout>

0 commit comments

Comments
 (0)