From 6769199b643f022f30d6e19503f935695f2b75e5 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Tue, 17 Sep 2024 14:32:32 +0300 Subject: [PATCH] fix: enhance checks in Adapters to avoid index errors --- .../mthmmy/activities/board/BoardAdapter.java | 24 +++++++------- .../downloads/DownloadsAdapter.java | 33 +++++++++++-------- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardAdapter.java index 6f8a33ce..ff5ed7a5 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardAdapter.java @@ -16,6 +16,7 @@ import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.util.ArrayList; @@ -63,6 +64,7 @@ class BoardAdapter extends RecyclerView.Adapter { return VIEW_TYPE_LOADING; } + @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_SUB_BOARD) { @@ -75,16 +77,16 @@ class BoardAdapter extends RecyclerView.Adapter { inflate(R.layout.activity_board_topic_row, parent, false); return new TopicViewHolder(topic); } - else if (viewType == VIEW_TYPE_LOADING) { + // viewType == VIEW_TYPE_LOADING + else { View loading = LayoutInflater.from(parent.getContext()). inflate(R.layout.recycler_loading_item, parent, false); return new LoadingViewHolder(loading); } - return null; } @Override - public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) { + public void onBindViewHolder(@NonNull final RecyclerView.ViewHolder holder, final int position) { if (holder instanceof SubBoardViewHolder) { final Board subBoard = parsedSubBoards.get(position); final SubBoardViewHolder subBoardViewHolder = (SubBoardViewHolder) holder; @@ -176,8 +178,9 @@ class BoardAdapter extends RecyclerView.Adapter { intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); context.startActivity(intent); }); - if (topicExpandableVisibility.get(topicViewHolder.getBindingAdapterPosition() - parsedSubBoards - .size())) { + + final int pos = topicViewHolder.getBindingAdapterPosition() - parsedSubBoards.size(); + if (pos >=0 && pos < topicExpandableVisibility.size() && topicExpandableVisibility.get(pos)) { topicViewHolder.topicExpandable.setVisibility(View.VISIBLE); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); } @@ -185,11 +188,11 @@ class BoardAdapter extends RecyclerView.Adapter { topicViewHolder.topicExpandable.setVisibility(View.GONE); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); } + topicViewHolder.showHideExpandable.setOnClickListener(view -> { - final int pos = topicViewHolder.getBindingAdapterPosition() - parsedSubBoards.size(); - if (pos >=0 && pos < topicExpandableVisibility.size()) { - final boolean visible = topicExpandableVisibility.get(topicViewHolder. - getBindingAdapterPosition() - parsedSubBoards.size()); + final int pos2 = topicViewHolder.getBindingAdapterPosition() - parsedSubBoards.size(); + if (pos2 >=0 && pos2 < topicExpandableVisibility.size()) { + final boolean visible = topicExpandableVisibility.get(pos2); if (visible) { topicViewHolder.topicExpandable.setVisibility(View.GONE); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); @@ -198,8 +201,7 @@ class BoardAdapter extends RecyclerView.Adapter { topicViewHolder.topicExpandable.setVisibility(View.VISIBLE); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); } - topicExpandableVisibility.set(topicViewHolder.getBindingAdapterPosition() - - parsedSubBoards.size(), !visible); + topicExpandableVisibility.set(pos2, !visible); } }); topicViewHolder.topicSubject.setTypeface(Typeface.createFromAsset(context.getAssets() diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsAdapter.java index 293fa457..b085b1d8 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsAdapter.java @@ -15,6 +15,7 @@ import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.recyclerview.widget.RecyclerView; import java.net.MalformedURLException; @@ -50,6 +51,7 @@ class DownloadsAdapter extends RecyclerView.Adapter { return (parsedDownloads.get(position) == null) ? VIEW_TYPE_LOADING : VIEW_TYPE_DOWNLOAD; } + @NonNull @Override public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { if (viewType == VIEW_TYPE_DOWNLOAD) { @@ -57,12 +59,12 @@ class DownloadsAdapter extends RecyclerView.Adapter { inflate(R.layout.activity_downloads_row, parent, false); return new DownloadViewHolder(download); } - else if (viewType == VIEW_TYPE_LOADING) { + // viewType == VIEW_TYPE_LOADING + else { View loading = LayoutInflater.from(parent.getContext()). inflate(R.layout.recycler_loading_item, parent, false); return new LoadingViewHolder(loading); } - return null; } @Override @@ -87,7 +89,9 @@ class DownloadsAdapter extends RecyclerView.Adapter { context.startActivity(intent); }); - if (downloadExpandableVisibility.get(downloadViewHolder.getAdapterPosition())) { + final int pos = downloadViewHolder.getBindingAdapterPosition(); + + if (pos >=0 && pos < downloadExpandableVisibility.size() && downloadExpandableVisibility.get(pos)) { downloadViewHolder.informationExpandable.setVisibility(View.VISIBLE); downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); } @@ -95,18 +99,21 @@ class DownloadsAdapter extends RecyclerView.Adapter { downloadViewHolder.informationExpandable.setVisibility(View.GONE); downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); } + downloadViewHolder.informationExpandableBtn.setOnClickListener(view -> { - final boolean visible = downloadExpandableVisibility.get(downloadViewHolder. - getAdapterPosition()); - if (visible) { - downloadViewHolder.informationExpandable.setVisibility(View.GONE); - downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); - } - else { - downloadViewHolder.informationExpandable.setVisibility(View.VISIBLE); - downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); + final int pos2 = downloadViewHolder.getBindingAdapterPosition(); + if (pos2 >=0 && pos2 < downloadExpandableVisibility.size()){ + final boolean visible = downloadExpandableVisibility.get(pos2); + if (visible) { + downloadViewHolder.informationExpandable.setVisibility(View.GONE); + downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); + } + else { + downloadViewHolder.informationExpandable.setVisibility(View.VISIBLE); + downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); + } + downloadExpandableVisibility.set(pos2, !visible); } - downloadExpandableVisibility.set(downloadViewHolder.getAdapterPosition(), !visible); }); downloadViewHolder.title.setTypeface(Typeface.createFromAsset(context.getAssets() , "fonts/fontawesome-webfont.ttf"));