Browse Source

fix: enhance checks in Adapters to avoid index errors

develop
Ezerous 3 months ago
parent
commit
6769199b64
  1. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/board/BoardAdapter.java
  2. 33
      app/src/main/java/gr/thmmy/mthmmy/activities/downloads/DownloadsAdapter.java

24
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.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList; import java.util.ArrayList;
@ -63,6 +64,7 @@ class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
return VIEW_TYPE_LOADING; return VIEW_TYPE_LOADING;
} }
@NonNull
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_SUB_BOARD) { if (viewType == VIEW_TYPE_SUB_BOARD) {
@ -75,16 +77,16 @@ class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
inflate(R.layout.activity_board_topic_row, parent, false); inflate(R.layout.activity_board_topic_row, parent, false);
return new TopicViewHolder(topic); return new TopicViewHolder(topic);
} }
else if (viewType == VIEW_TYPE_LOADING) { // viewType == VIEW_TYPE_LOADING
else {
View loading = LayoutInflater.from(parent.getContext()). View loading = LayoutInflater.from(parent.getContext()).
inflate(R.layout.recycler_loading_item, parent, false); inflate(R.layout.recycler_loading_item, parent, false);
return new LoadingViewHolder(loading); return new LoadingViewHolder(loading);
} }
return null;
} }
@Override @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) { if (holder instanceof SubBoardViewHolder) {
final Board subBoard = parsedSubBoards.get(position); final Board subBoard = parsedSubBoards.get(position);
final SubBoardViewHolder subBoardViewHolder = (SubBoardViewHolder) holder; final SubBoardViewHolder subBoardViewHolder = (SubBoardViewHolder) holder;
@ -176,8 +178,9 @@ class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
context.startActivity(intent); 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.topicExpandable.setVisibility(View.VISIBLE);
topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp);
} }
@ -185,11 +188,11 @@ class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
topicViewHolder.topicExpandable.setVisibility(View.GONE); topicViewHolder.topicExpandable.setVisibility(View.GONE);
topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp);
} }
topicViewHolder.showHideExpandable.setOnClickListener(view -> { topicViewHolder.showHideExpandable.setOnClickListener(view -> {
final int pos = topicViewHolder.getBindingAdapterPosition() - parsedSubBoards.size(); final int pos2 = topicViewHolder.getBindingAdapterPosition() - parsedSubBoards.size();
if (pos >=0 && pos < topicExpandableVisibility.size()) { if (pos2 >=0 && pos2 < topicExpandableVisibility.size()) {
final boolean visible = topicExpandableVisibility.get(topicViewHolder. final boolean visible = topicExpandableVisibility.get(pos2);
getBindingAdapterPosition() - parsedSubBoards.size());
if (visible) { if (visible) {
topicViewHolder.topicExpandable.setVisibility(View.GONE); topicViewHolder.topicExpandable.setVisibility(View.GONE);
topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp);
@ -198,8 +201,7 @@ class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
topicViewHolder.topicExpandable.setVisibility(View.VISIBLE); topicViewHolder.topicExpandable.setVisibility(View.VISIBLE);
topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); topicViewHolder.showHideExpandable.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp);
} }
topicExpandableVisibility.set(topicViewHolder.getBindingAdapterPosition() - topicExpandableVisibility.set(pos2, !visible);
parsedSubBoards.size(), !visible);
} }
}); });
topicViewHolder.topicSubject.setTypeface(Typeface.createFromAsset(context.getAssets() topicViewHolder.topicSubject.setTypeface(Typeface.createFromAsset(context.getAssets()

33
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.LinearLayout;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView; import androidx.recyclerview.widget.RecyclerView;
import java.net.MalformedURLException; import java.net.MalformedURLException;
@ -50,6 +51,7 @@ class DownloadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
return (parsedDownloads.get(position) == null) ? VIEW_TYPE_LOADING : VIEW_TYPE_DOWNLOAD; return (parsedDownloads.get(position) == null) ? VIEW_TYPE_LOADING : VIEW_TYPE_DOWNLOAD;
} }
@NonNull
@Override @Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_DOWNLOAD) { if (viewType == VIEW_TYPE_DOWNLOAD) {
@ -57,12 +59,12 @@ class DownloadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
inflate(R.layout.activity_downloads_row, parent, false); inflate(R.layout.activity_downloads_row, parent, false);
return new DownloadViewHolder(download); return new DownloadViewHolder(download);
} }
else if (viewType == VIEW_TYPE_LOADING) { // viewType == VIEW_TYPE_LOADING
else {
View loading = LayoutInflater.from(parent.getContext()). View loading = LayoutInflater.from(parent.getContext()).
inflate(R.layout.recycler_loading_item, parent, false); inflate(R.layout.recycler_loading_item, parent, false);
return new LoadingViewHolder(loading); return new LoadingViewHolder(loading);
} }
return null;
} }
@Override @Override
@ -87,7 +89,9 @@ class DownloadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
context.startActivity(intent); 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.informationExpandable.setVisibility(View.VISIBLE);
downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp);
} }
@ -95,18 +99,21 @@ class DownloadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
downloadViewHolder.informationExpandable.setVisibility(View.GONE); downloadViewHolder.informationExpandable.setVisibility(View.GONE);
downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp);
} }
downloadViewHolder.informationExpandableBtn.setOnClickListener(view -> { downloadViewHolder.informationExpandableBtn.setOnClickListener(view -> {
final boolean visible = downloadExpandableVisibility.get(downloadViewHolder. final int pos2 = downloadViewHolder.getBindingAdapterPosition();
getAdapterPosition()); if (pos2 >=0 && pos2 < downloadExpandableVisibility.size()){
if (visible) { final boolean visible = downloadExpandableVisibility.get(pos2);
downloadViewHolder.informationExpandable.setVisibility(View.GONE); if (visible) {
downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp); downloadViewHolder.informationExpandable.setVisibility(View.GONE);
} downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_down_accent_24dp);
else { }
downloadViewHolder.informationExpandable.setVisibility(View.VISIBLE); else {
downloadViewHolder.informationExpandableBtn.setImageResource(R.drawable.ic_arrow_drop_up_accent_24dp); 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() downloadViewHolder.title.setTypeface(Typeface.createFromAsset(context.getAssets()
, "fonts/fontawesome-webfont.ttf")); , "fonts/fontawesome-webfont.ttf"));

Loading…
Cancel
Save