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.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
@ -63,6 +64,7 @@ class BoardAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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()

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.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
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;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == VIEW_TYPE_DOWNLOAD) {
@ -57,12 +59,12 @@ class DownloadsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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"));

Loading…
Cancel
Save