From 5de855fe0e01ef7de648673174e25e01674212fe Mon Sep 17 00:00:00 2001 From: Ezerous Date: Tue, 25 Aug 2020 15:25:32 +0300 Subject: [PATCH] TopicAdapter crash fix --- .../profile/summary/SummaryFragment.java | 1 - .../mthmmy/activities/topic/TopicAdapter.java | 84 +++++-------------- 2 files changed, 21 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java index ea257924..749f42bc 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/summary/SummaryFragment.java @@ -21,7 +21,6 @@ import org.jsoup.select.Elements; import java.util.HashMap; import java.util.LinkedHashMap; -import java.util.Objects; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index def64530..0584192f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -696,43 +696,13 @@ class TopicAdapter extends RecyclerView.Adapter { } holder.replyEditor.setText(replyText); holder.replyEditor.getEditText().setSelection(holder.replyEditor.getText().length()); - holder.replyEditor.getEditText().addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - ((Post) topicItems.get(holder.getAdapterPosition())).setBbContent(charSequence.toString()); - } - - @Override - public void afterTextChanged(Editable editable) { - - } - }); + holder.replyEditor.getEditText().addTextChangedListener(createTextWatcher(holder)); if (backPressHidden) { holder.replyEditor.requestEditTextFocus(); backPressHidden = false; } - holder.quickReplySubject.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - ((Post) topicItems.get(holder.getAdapterPosition())).setSubject(charSequence.toString()); - } - - @Override - public void afterTextChanged(Editable editable) { - - } - }); + holder.quickReplySubject.addTextChangedListener(createTextWatcher(holder)); } else if (currentHolder instanceof EditMessageViewHolder) { final EditMessageViewHolder holder = (EditMessageViewHolder) currentHolder; @@ -767,38 +737,9 @@ class TopicAdapter extends RecyclerView.Adapter { viewModel.editPost(position, holder.editSubject.getText().toString(), holder.editEditor.getText().toString()); }); - holder.editSubject.addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - ((Post) topicItems.get(holder.getAdapterPosition())).setSubject(charSequence.toString()); - } - - @Override - public void afterTextChanged(Editable editable) { - - } - }); - holder.editEditor.getEditText().addTextChangedListener(new TextWatcher() { - @Override - public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { - - } - - @Override - public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - ((Post) topicItems.get(holder.getAdapterPosition())).setBbContent(charSequence.toString()); - } - - @Override - public void afterTextChanged(Editable editable) { + holder.editSubject.addTextChangedListener(createTextWatcher(holder)); + holder.editEditor.getEditText().addTextChangedListener(createTextWatcher(holder)); - } - }); if (backPressHidden) { holder.editEditor.requestEditTextFocus(); backPressHidden = false; @@ -807,6 +748,23 @@ class TopicAdapter extends RecyclerView.Adapter { } } + private TextWatcher createTextWatcher(@NonNull final RecyclerView.ViewHolder holder){ + return new TextWatcher() { + @Override + public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) { } + + @Override + public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { + int position = holder.getAdapterPosition(); + if (position >= 0 && position < topicItems.size()) + ((Post) topicItems.get(position)).setBbContent(charSequence.toString()); + } + + @Override + public void afterTextChanged(Editable editable) { } + }; + } + private void loadAvatar(String imageUrl, ImageView imageView, Context context) { if(imageUrl!=null) imageUrl = imageUrl.trim();